fix: resolve async SQLAlchemy lazy loading issue in cart service
Use get_cart() with selectinload instead of refresh() to avoid greenlet_spawn errors when accessing cart.items relationship. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
286eb2f06c
commit
169b63160d
|
|
@ -21,7 +21,13 @@ class CartService:
|
|||
cart = Cart()
|
||||
self.db.add(cart)
|
||||
await self.db.commit()
|
||||
await self.db.refresh(cart)
|
||||
# Re-fetch with items loaded to avoid lazy loading issues
|
||||
result = await self.db.execute(
|
||||
select(Cart)
|
||||
.where(Cart.id == cart.id)
|
||||
.options(selectinload(Cart.items))
|
||||
)
|
||||
cart = result.scalar_one()
|
||||
return self._cart_to_response(cart)
|
||||
|
||||
async def get_cart(self, cart_id: UUID) -> CartResponse | None:
|
||||
|
|
@ -59,8 +65,7 @@ class CartService:
|
|||
):
|
||||
existing.quantity += item.quantity
|
||||
await self.db.commit()
|
||||
await self.db.refresh(cart)
|
||||
return self._cart_to_response(cart)
|
||||
return await self.get_cart(cart_id)
|
||||
|
||||
# Add new item
|
||||
cart_item = CartItem(
|
||||
|
|
@ -73,8 +78,7 @@ class CartService:
|
|||
)
|
||||
self.db.add(cart_item)
|
||||
await self.db.commit()
|
||||
await self.db.refresh(cart)
|
||||
return self._cart_to_response(cart)
|
||||
return await self.get_cart(cart_id)
|
||||
|
||||
async def update_item(
|
||||
self,
|
||||
|
|
|
|||
Loading…
Reference in New Issue