sqlmodel icon indicating copy to clipboard operation
sqlmodel copied to clipboard

Added Documentation for usage with async tools. Fixes #626

Open deshetti opened this issue 2 years ago • 7 comments
trafficstars

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

deshetti avatar Aug 08 '23 15:08 deshetti

📝 Docs preview for commit a5fe1d198fa60e82d11efd95770ec1203b9afb9b at: https://ececea1e.sqlmodel.pages.dev

github-actions[bot] avatar Aug 08 '23 15:08 github-actions[bot]

📝 Docs preview for commit b0081f5a01f23cc0b808f2e361d0a593b1eeef9f at: https://049f644f.sqlmodel.pages.dev

github-actions[bot] avatar Aug 09 '23 12:08 github-actions[bot]

@tiangolo , are you merging this in this century or next?

StashOfCode avatar Feb 08 '24 09:02 StashOfCode

@tiangolo Why don't you add the contribution?

moralespanitz avatar Apr 10 '24 21:04 moralespanitz

Interesting approach. Why not use async_sessionmaker?

vduseev avatar Jul 11 '24 10:07 vduseev

The async session part doesn't work for me and gives an error when executing statement.

https://github.com/deshetti/sqlmodel-async-example/blob/e412da00f557a352ee3f66a8545ef11401ea5dde/main.py#L42-L47

# 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: results= await session.exec(statement)

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: Unexpected type(s): (Any, Type[AsyncSession], bool) Possible type(s): (Connection | Connection | Engine | Engine | None, None, bool) (Connection | Connection | Engine | Engine | AsyncConnection | AsyncConnection | AsyncEngine | AsyncEngine | None, Type[_TSessionMakerType], bool) (Connection | Connection | Engine | Engine | None, None, bool) (Connection | Connection | Engine | Engine | None, Type[_TSessionMakerType], bool) for the sessionmaker

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: https://github.com/tiangolo/fastapi/discussions/9054#discussioncomment-5156427

version: FastAPI: 0.111.0 sqlmodel: 0.0.19 But this already happens with older versions, even if I use the latest version.

Lexachoc avatar Jul 11 '24 14:07 Lexachoc