Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do you support importing Asynchronous Sessions from sqlmodel? #1218

Open
1 task done
yuanjie-ai opened this issue Nov 18, 2024 · 4 comments
Open
1 task done

Do you support importing Asynchronous Sessions from sqlmodel? #1218

yuanjie-ai opened this issue Nov 18, 2024 · 4 comments

Comments

@yuanjie-ai
Copy link

Privileged issue

  • I'm @tiangolo or he asked me directly to create an issue here.

Issue Content

from sqlmodel import AsyncSession

@AtticusZeller
Copy link

AtticusZeller commented Nov 19, 2024

here for you

from sqlmodel.ext.asyncio.session import AsyncSession

you can search more info in discussion if interested

@supermacro
Copy link

Relatedly, suppose I wanted to use sqlmodel with https://fastapi-users.github.io/ ... the docs require that I create an async engine via create_async_engine... How can I integrate this with sqlmodel?

More generally, want to learn how to use async io with sqlmodel

@AtticusZeller
Copy link

AtticusZeller commented Jan 31, 2025

Relatedly, suppose I wanted to use sqlmodel with https://fastapi-users.github.io/ ... the docs require that I create an async engine via create_async_engine... How can I integrate this with sqlmodel?

More generally, want to learn how to use async io with sqlmodel

  1. sqlmodel is built upon SQLAlchemy, so create_async_engine can normally work with sqlmodel without conflicts.

  2. i quit async orm because it annoying to write test for them

@ekalosak
Copy link

ekalosak commented Feb 18, 2025

Using the create_async_engine, I run into this issue:

ValueError: the greenlet library is required to use this function. No module named 'greenlet'

tl;dr

  1. pip install greenlet asyncpg
  2. Make sure your DB URL is specified like this: [1.4] The asyncio extension requires an async driver to be used. The loaded 'psycopg2' is not async. sqlalchemy/sqlalchemy#6046 (comment)
  3. Setup like this:
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.orm import sessionmaker
from sqlmodel.ext.asyncio.session import AsyncSession
_engine = create_async_engine(PG_URL, echo=PG_VERBOSE)
_async_session_factory = sessionmaker(_engine, class_=AsyncSession)
async with _engine.begin() as conn:
    await conn.run_sync(SQLModel.metadata.create_all)
  1. In your code, interact with sessions like this:
async with _async_session_factory() as session:
    heroes = await session.exec(select(Hero))
heroes = heroes.all()

Troubleshooting

Example code

Environment

  • Python 3.12.9
  • sqlalchemy 2.0.38
  • sqlmodel 0.0.22

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants