-
-
Notifications
You must be signed in to change notification settings - Fork 685
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
Added Documentation for usage with async tools. Fixes #626 #633
base: main
Are you sure you want to change the base?
Conversation
📝 Docs preview for commit a5fe1d1 at: https://ececea1e.sqlmodel.pages.dev |
📝 Docs preview for commit b0081f5 at: https://049f644f.sqlmodel.pages.dev |
``` | ||
|
||
## Final code | ||
``` |
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.
``` | |
```python |
``` | ||
pip install sqlmodel asyncpg fastapi uvicorn | ||
``` |
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.
``` | |
pip install sqlmodel asyncpg fastapi uvicorn | |
``` | |
```bash | |
pip install sqlmodel asyncpg fastapi uvicorn |
) | ||
|
||
|
||
# Ayschronous Context manager for handling database sessions |
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.
# Ayschronous Context manager for handling database sessions | |
# Asynchronous Context manager for handling database sessions |
@tiangolo , are you merging this in this century or next? |
@tiangolo Why don't you add the contribution? |
Interesting approach. Why not use |
The async session part doesn't work for me and gives an error when executing statement.
error occurs at: within an endpoint where the session is a dependency: error message: Also, there is a warning in Pycharm that says: To overcome this, I use directly the
which works for me when using the async session as dependencies. Hope this helps others or do you have an idea why the original solution doesn't work? This seems to be the reason for the error or is related to it: fastapi/fastapi#9054 (comment) version: |
# Endpoint to create a new user | ||
@app.post("/users/", response_model=User) | ||
async def create_user_endpoint(user: UserCreate): | ||
db_user = User(**user.dict()) |
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.
.dict()
is deprecated for model_dump()
db_user = User(**user.dict()) | |
db_user = User(**user.model_dump()) |
```python | ||
@app.post("/users/", response_model=User) | ||
async def create_user_endpoint(user: UserCreate): | ||
db_user = User(**user.dict()) |
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.
.dict() is deprecated for model_dump()
db_user = User(**user.dict()) | |
db_user = User(**user.model_dump()) |
@app.post("/users/", response_model=User) | ||
async def create_user_endpoint(user: UserCreate): | ||
db_user = User(**user.dict()) | ||
result = await create_user(db_user) | ||
return result |
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.
Maybe drop the create_user()
function and just have everything in here. We also get the added benefit of showing of dependency injection, we are in the advanced section after all.
@app.post("/users/", response_model=User) | |
async def create_user_endpoint(user: UserCreate): | |
db_user = User(**user.dict()) | |
result = await create_user(db_user) | |
return result | |
@app.post("/users/", response_model=User) | |
async def create_user_endpoint( | |
user: UserCreate, | |
session: AsyncSession = Depends(get_session), | |
): | |
db_user = User(user.model_dump()) | |
session.add(db_user) | |
await session.commit() | |
await session.refresh(db_user) | |
return db_user |
When will this be released? |
|
||
from fastapi import FastAPI | ||
from pydantic import BaseModel | ||
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine |
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.
Can use
sqlmodel/sqlmodel/ext/asyncio/session.py
Line 32 in 0c65fed
class AsyncSession(_AsyncSession): |
I have spent a good amount of time to figure out async implementation with SQLModel & FastAPI in one of our projects and thought this would be a helpful guide for anyone who is looking to do the same in their projects.
A simple working project is available here to test. Running the docker-compose will start the postgres with the required database: https://github.com/deshetti/sqlmodel-async-example