| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
|
📝 Docs preview for commit a5fe1d1 at: https://ececea1e.sqlmodel.pages.dev |
Sorry, something went wrong.
|
📝 Docs preview for commit b0081f5 at: https://049f644f.sqlmodel.pages.dev |
Sorry, something went wrong.
| ``` | ||
|
|
||
| ## Final code | ||
| ``` |
There was a problem hiding this comment.
| ``` | |
| ```python |
Sorry, something went wrong.
| ``` | ||
| 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 |
Sorry, something went wrong.
| ) | ||
| # 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 |
Sorry, something went wrong.
|
@tiangolo , are you merging this in this century or next? |
Sorry, something went wrong.
|
Interesting approach. Why not use async_sessionmaker? |
Sorry, something went wrong.
|
The async session part doesn't work for me and gives an error when executing statement. # Ayschronous Context manager for handling database sessions
@asynccontextmanager
async def get_session() -> AsyncSession:
async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
async with async_session() as session:
yield session
error occurs at: within an endpoint where the session is a dependency: session: AsyncSession = Depends(get_session) error message: '_AsyncGeneratorContextManager' object has no attribute 'exec' Also, there is a warning in Pycharm that says: To overcome this, I use directly the AsyncSession instead of sessionmaker: async def get_session() -> AsyncSession:
async with AsyncSession(data_engine) as session:
yield session
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: |
Sorry, something went wrong.
| # 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()) |
Sorry, something went wrong.
| ```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()) |
Sorry, something went wrong.
| @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 |
Sorry, something went wrong.
|
When will this be released? |
Sorry, something went wrong.
|
|
||
| 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
Sorry, something went wrong.
|
This pull request has a merge conflict that needs to be resolved. |
Sorry, something went wrong.
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.
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
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