0

My schemas:

class ChannelIn(BaseModel):
    name: str
    avatar: Optional[str]
    description: Optional[str]

    class Config:
        arbitrary_types_allowed = True
        orm_mode = True

class ChannelResponse(ChannelIn):
    id: UUID
    created: datetime
    updated: Optional[datetime]
    user_id: UUID
    posts: List[PostResponse]

My endpoint:

@channel_router.get('/', response_model=List[ChannelResponse])
async def get_all_channels(session: AsyncSession = Depends(get_session)) -> List[ChannelResponse]:
    # results = await session.execute(select(Channel).where(Channel.user.has(User.username == user)))
    results = await session.execute(select(Channel))
    return list(results.scalars().all())

Just I added this string: posts: List[PostResponse] in my schema, I get missing.greenlel error. Before that it was ok. Why?

  • 1
    Have you seen https://stackoverflow.com/questions/74252768/missinggreenlet-greenlet-spawn-has-not-been-called ? The problem is probably because `ChannelResponse` then tries to lazy-load `PostReponse`, which isn't happening in an async context. Define a lazy-loading strategy that fetches the relevant PostResponse objects at query time. – MatsLindh Mar 23 '23 at 09:33
  • 2
    In addition to what @MatsLindh wrote, here is the [SQLAlchemy documentation](https://docs.sqlalchemy.org/en/20/orm/extensions/asyncio.html#preventing-implicit-io-when-using-asyncsession) on how to avoid implicit IO when using `AsyncSession`. – M.O. Mar 23 '23 at 09:42

1 Answers1

0

You just need to add a parameter in your sqlalchemy relationship: lazy='selectin'