Added Documentation for usage with async tools. Fixes #626#633
Added Documentation for usage with async tools. Fixes #626#633deshetti wants to merge 3 commits into
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 | ||
| ``` |
| ``` | ||
| pip install sqlmodel asyncpg fastapi uvicorn | ||
| ``` |
There was a problem hiding this comment.
| ``` | |
| 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.
| # 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.
.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.
.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.
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.
Can use
sqlmodel/sqlmodel/ext/asyncio/session.py
Line 32 in 0c65fed
|
This pull request has a merge conflict that needs to be resolved. |
svlandeg
left a comment
There was a problem hiding this comment.
Hi @deshetti, all,
Thanks for the contribution 🙏
We've discussed this internally and decided that we prefer to write the first draft of this new documentation section ourselves. We are now tracking this on our internal TODO list. We'll go ahead and close this one in the meantime.
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