Replies: 7 comments
|
Look at issue #54 You can get an Async enginedirectly from SQLAlchemy Looking at the SQLmodel code directly the engine is not async |
|
Just 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: short version: Has anybody bumped into this ? |
|
@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 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... 😄 |
|
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 |
from typing import Optional
import pytest
from sqlmodel import Field, SQLModel
from sqlmodel.ext.asyncio.session import AsyncSession
from sqlalchemy.ext.asyncio import create_async_engine
from sqlmodel import select
# sqlite for example
db_path = "database.sqlite"
uri = f"sqlite+aiosqlite:///{db_path}"
engine = create_async_engine(uri, echo=True)
class Hero(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
name: str
secret_name: str
age: Optional[int] = None
@pytest.fixture(autouse=True)
async def init_models():
async with engine.begin() as conn:
await conn.run_sync(SQLModel.metadata.drop_all)
await conn.run_sync(SQLModel.metadata.create_all)
async def test_async_session():
async with AsyncSession(engine) as session:
data_to_add = Hero(name="Deadpond", secret_name="Dive Wilson")
session.add(data_to_add)
stmt = select(Hero).where(Hero.name == "Deadpond") # whatever you need to do
results = await session.exec(stmt)
data_to_del = results.first()
await session.delete(data_to_del)
await session.commit() |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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
All reactions