Repository navigation
Is SQLModel naturally async aware ? #129
Description
Activity
- addedquestionFurther information is requestedFurther information is requested
on Oct 12, 2021 Look at issue #54
You can get an Async enginedirectly from SQLAlchemy
Looking at the SQLmodel code directly the engine is not async
Reacted by joaopfgJust Jumping in on the conversation for the sake of not raising another issue:
Using SQLModel's version of AsyncSession and stumbled by this warning while running some tests on the code:
/home/user/git_repos/project/venv/lib/python3.9/site-packages/sqlmodel/orm/session.py:60: SAWarning: Class SelectOfScalar will not make use of SQL compilation caching as it does not set the 'inherit_cache' attribute to ``True``. This can have significant performance implications including some performance degradations in comparison to prior SQLAlchemy versions. Set this attribute to True if this object can make use of the cache key generated by the superclass. Alternatively, this attribute may be set to False which will disable this warning. (Background on this error at: https://sqlalche.me/e/14/cprf) results = super().execute( -- Docs: https://docs.pytest.org/en/stable/warnings.htmlshort version:
Class SelectOfScalar will not make use of SQL compilation caching as it does not set the 'inherit_cache' attribute to ``True``Has anybody bumped into this ?
Reacted by Nikolay Seliverstov and Alexandru Dreptu@raulolteanu-sudo Check out #189, the workaround at the end should help with that.
https://testdriven.io/blog/fastapi-sqlmodel/
The author is using SQLAlchemy's async engine and session rather than SQLModel's to execute the queries; hence
session.execute()rather thansession.exec(). Consequently you lose some of the mypy typing benefits that SQLModel brings, but looks like it otherwise works.On the other hand, it would be nice to bring native support into SQLModel, especially since @tiangolo's other project, FastAPI, is all about asyncing all the things... 😄
Reacted by Mauricio, a5r0n, iron3oxide, Austin Hallett, Dmitry Mazur, espdev, Paul Garner, pavdwest, Gianfranco Cossani Morales, Eren and 10 moreAny updates?
Any updates?
Currently I'm using
from sqlmodel.ext.asyncio.session import AsyncSession from sqlalchemy.ext.asyncio import create_async_engine from sqlmodel import select # sqlite for example db_path = "/path/to/db" uri = f"sqlite+aiosqlite:///{db_path}" aengine = create_async_engine(uri, echo=True) async with AsyncSession(aengine) as session: stmt = select(***) # whatever you need to do results = await session.exec(stmt) data_to_add = YourModel(assign value here) session.add(data_to_add) data_to_del = results.first() await session.delete(data_to_del) await session.commit()
I also hope the
asynctutorial would be finished as soon as possibleReacted by Baivo, Fibert Loyee, xiaoyuzju, Sanjin Dedic, synodriver, Parampal Singh, Nate Oelke, John Syin, iraj jelodari, Adam Collard and 3 more- locked and limited conversation to collaborators
on May 18, 2026
First Check
Commit to Help
Example Code
Description
Can I use SQLModel inside my FastAPI async endpoint implementations without problems ? Apparently it works. But I'm not sure if it's destroying the asynchronicity.
Operating System
Linux
Operating System Details
No response
SQLModel Version
0.0.4
Python Version
3.9.5
Additional Context
No response