Add an AwaitableField for accessing sqlmodel field asynchronously#872
Add an AwaitableField for accessing sqlmodel field asynchronously#8722jun0 wants to merge 6 commits intofastapi:mainfrom
Conversation
|
📝 Docs preview for commit 2d9af2f at: https://bfab4bb5.sqlmodel.pages.dev |
|
📝 Docs preview for commit 548f005 at: https://d4117a75.sqlmodel.pages.dev |
|
📝 Docs preview for commit 1d7f73e at: https://b18eab40.sqlmodel.pages.dev |
|
@tiangolo this is very useful. My workaround now is a ton of lazy loads |
|
any news on this issue? |
|
This pull request has a merge conflict that needs to be resolved. |
YuriiMotov
left a comment
There was a problem hiding this comment.
@2jun0, thank you for your interest and efforts!
My personal opinions is that we don't need this.
This gives an alternative to lazy load instructions\declarations, but the syntax is not nice:
- we have to declare all types as
Awaitable[str] - we have to add
awaitevery time we access field
Working with async SQLAlchemy (and SQLModel) is more difficult, but even with suggested changes people will still need to learn it to avoid issues with accessing fields when DB session has already been closed, and other situations.
Also, lazy loading instructions\declarations will be more efficient in terms of performance.
Thanks again, but I think we should close this PR.
|
@YuriiMotov Understood, thank you for reviewing :) |
I'm not that good at English. So, if there's anything you don't understand, please feel free to reply.
Currently, accessing some fields asynchronously is hard. I often encounter the MissingGreenlet error while programming asynchronously, which happens because attempting IO without using await keyword when accessing lazy loading or expired fields. (others seem to have this problem too #868 #74)
While SQLAlchemy provides the AsyncAttr Mixin, I found it not suitable for sqlmodel because the completion wasn't good enough.
So, I propose an AwaitableField, making access to other fields awaitable like below:
Usage
Create a AsyncSQLModel and add an awaitable field
You can easliy create a AsyncSQLModel Using the same interface as a sqlmodel. and an AwaitableField yields an awaitable field for the field specified in the argument.
This allows fields which may be subject to lazy loading or deferred / unexpiry loading to be accessed like this:
Access a Relationship Field using an AwaitableField
Using an AwaitableField with Relationship fields can resolve the problem encountered during lazy loading