-
-
Notifications
You must be signed in to change notification settings - Fork 7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add asynchronous sqlalchemy example #2331
base: master
Are you sure you want to change the base?
Conversation
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## master #2331 +/- ##
===========================================
- Coverage 100.00% 98.57% -1.43%
===========================================
Files 540 415 -125
Lines 13969 10412 -3557
===========================================
- Hits 13969 10264 -3705
- Misses 0 148 +148 ☔ View full report in Codecov by Sentry. |
Added an issue to pydantic for the response_model: pydantic/pydantic#2124 The test added here fails because of this issue. |
📝 Docs preview for commit 0eb012e at: https://5fb02820cf2b1c96da914104--fastapi.netlify.app |
Can you push the failing commit so I can debug? 🥺 |
@ArcLightSlavik you need to use |
📝 Docs preview for commit db9b2cf at: https://5fb0d245fcd4be0010bc0424--fastapi.netlify.app |
📝 Docs preview for commit fe9b662 at: https://5fb0d37199973915dc505d90--fastapi.netlify.app |
📝 Docs preview for commit 0811c04 at: https://5fb2ba7646867616716f246c--fastapi.netlify.app |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found a problem, it comes from Sqlalchemy. And the problem will appear in PostgreSQL.
When the crud is operated for the second time, the first time object will become unavailable.
Because sqlalchemy need to query again, but it is not await at this time.
@yenchenLiu Haven't written about this issue here, but it's been known for weeks now. I am not sure which dependancy is at fault for it so I haven't made an issue. I assume it will be fixed in beta/release version of async alchemy. |
SQLAlchemy released a 2nd beta and from my testing everything seems to be working. @yenchenLiu Could you install |
Did a quick test but SQLAlchemy complained that the default sqlite driver was not async. Does there even exist an async sqlite driver? |
FYI: SQLAlchemy 1.4 has just been released. @jokull from the release docs I think not:
|
Hi @ArcLightSlavik, I tried this with PostgreSQL in the past (1.4.0b1) and with SQLAlchemy 1.4.1 today, and it doesn't work properly as @yenchenLiu mentions. A workaround I found is to use
Also is necessary to remove |
@kurtrottmann indeed. Relationships don't work quite the same in async mode as stated in the async sqlalchemy documentation: here |
Maybe adding an |
Thank you so much @ArcLightSlavik for writing this! It was instrumental in my attempt to get the async ORM working in my app. I hope this makes it to the official docs soon so others can benefit from it. Also thanks to @kurtrottmann for your tips, they likely saved me a lot of time. I have several levels of nested relationships in my app, so I ended up declaring my relationships using
I did that because putting |
1.4.3 was released today with aiosqlite support. |
@ArcLightSlavik i have ready to use here solution: https://github.com/grillazz/fastapi-sqlalchemy-asyncpg |
📝 Docs preview for commit de5eab2 at: https://6060f61a45aa8bf9b4eecf14--fastapi.netlify.app |
Don't have time to add the Update method, so if anyone wants to do that I would be very happy 🙂 |
@ArcLightSlavik is below model method a solution for pass update for you: |
@grillazz Did you tagged correct person in above comment? I am bit out of context here 😄 |
sorry my bad |
@grillazz Not really, I want the main insparation taken from https://github.com/tiangolo/full-stack-fastapi-postgresql/blob/master/%7B%7Bcookiecutter.project_slug%7D%7D/backend/app/app/crud/crud_user.py#L27, but without the usage of classes.
|
The get_db dependency causes a weird Here's my code: db.py
session.py
|
@joweenflores below is working well for me:
|
Already in #3096 but fix was using |
📝 Docs preview for commit a6ce99c at: https://6081db5166760a00dedd6827--fastapi.netlify.app |
Currently, sqlalchemy 1.4 is not allowed by pyproject.toml. I suppose that restriction would need to be loosened as well. |
@musicinmybrain That would be a separate PR, it doesn't affect end users only the tests in this project. |
Actually, @musicinmybrain is right. Well, in summary, the version needs to change. 😗 |
Eh fair I guess, although the sound of tests for documentation scares me 😄 |
In the case of this PR, I guess you just need to reimport the test function, you can check my PR above or click here. |
If someone could do it I would be happy, don't have time to look into it for a while 😢 |
Any updates on this PR? |
📝 Docs preview for commit 982da54 at: https://61604430f990703b9c0e6678--fastapi.netlify.app |
982da54
to
3025577
Compare
3025577
to
ce89c33
Compare
📝 Docs preview for commit ce89c33 at: https://626eec6527fc40141e35602a--fastapi.netlify.app |
Recently sqlalchemy released the beta for the 1.4 release, which includes support for asynchronous calls.
This PR adds an example based on (https://fastapi.tiangolo.com/tutorial/sql-databases/) but with asynchronous calls.