kuzxnia / async_factory_boy

factory_boy extension with asynchronous ORM support
https://pypi.org/project/async-factory-boy/
MIT License
18 stars 7 forks source link

Issue with using expire_on_commit=True in async_sessionmaker #9

Open SharksBoy opened 6 months ago

SharksBoy commented 6 months ago

Hello! I would also like to thank you for this project! Don't judge strictly, this is my first open source Issue.

I found that in the current version, if you use _asyncsessionmaker or sessionmaker with the expire_on_commit=True parameter (btw it's by default), an error occurs when trying to create an instance of an ORM Model object.

sqlalchemy.exc.MissingGreenlet: greenlet_spawn has not been called; can't call await_only() here. Was IO attempted in an unexpected place? (Background on this error at: https://sqlalche.me/e/20/xd2s)

This happens because the object expires after a commit (which is obvious from the error). My suggestion: add session.refresh(instance) to the create function after session.commit(). This should help and work in both cases of arguments.