sqlalchemyfastapigreenlets

missing greenlet in schema response model fastapi


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?


Solution

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