Skip to content

MissingGreenlet exception while creating both parent and child instances and returning parent #1323

Closed Answered by YuriiMotov
ArazHeydarov asked this question in Questions
Discussion options

You must be logged in to vote

At the moment FastAPI validates response, your DB session is closed, but child objects are not loaded by default and sqlalchemy will try to load children. But this will happen in non-async context and that will cause that error.

To avoid this error you should pre-load child objects using selectinload:

from sqlalchemy.orm import selectinload

...
        parent = await session.get(Parent, 1, options=[selectinload(Parent.children)])

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
2 participants