Skip to content

Commit a1e82c3

Browse files
committed
docs: add async tutorial and fix incorrect async usage examples in docstrings
1 parent 5611bda commit a1e82c3

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

docs/tutorial/async/index.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Async SQLModel
2+
3+
SQLModel supports asynchronous database operations out of the box using SQLAlchemy's async features and the `AsyncSession` provided by SQLModel.
4+
5+
## When to use Async
6+
7+
Asynchronous programming is particularly useful for Web APIs (like FastAPI) that handle many concurrent connections and spend much of their time waiting for the database to respond.
8+
9+
## Create the Async Engine
10+
11+
To use async, you need an async database driver (like `aiosqlite` for SQLite or `asyncpg` for PostgreSQL).
12+
13+
```python
14+
from sqlalchemy.ext.asyncio import create_async_engine
15+
from sqlmodel import SQLModel
16+
17+
sqlite_url = "sqlite+aiosqlite:///database.db"
18+
engine = create_async_engine(sqlite_url, echo=True)
19+
20+
async def init_db():
21+
async with engine.begin() as conn:
22+
await conn.run_sync(SQLModel.metadata.create_all)
23+
```
24+
25+
## Use AsyncSession
26+
27+
Use `from sqlmodel.ext.asyncio.session import AsyncSession` to perform async operations.
28+
29+
```python
30+
from sqlmodel.ext.asyncio.session import AsyncSession
31+
from sqlmodel import select
32+
33+
async def get_heroes():
34+
async with AsyncSession(engine) as session:
35+
statement = select(Hero)
36+
results = await session.exec(statement)
37+
return results.all()
38+
```
39+
40+
## Integration with FastAPI
41+
42+
In FastAPI, you can use a dependency to provide an `AsyncSession`:
43+
44+
```python
45+
async def get_session():
46+
async with AsyncSession(engine) as session:
47+
yield session
48+
```

sqlmodel/ext/asyncio/session.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ async def exec(
116116
For example:
117117
118118
```Python
119-
heroes = await session.execute(select(Hero)).scalars().all()
119+
heroes = (await session.execute(select(Hero))).scalars().all()
120120
```
121121
122122
instead you could use `exec()`:
123123
124124
```Python
125-
heroes = await session.exec(select(Hero)).all()
125+
heroes = (await session.exec(select(Hero))).all()
126126
```
127127
"""
128128
)
@@ -145,13 +145,13 @@ async def execute(
145145
For example:
146146
147147
```Python
148-
heroes = await session.execute(select(Hero)).scalars().all()
148+
heroes = (await session.execute(select(Hero))).scalars().all()
149149
```
150150
151151
instead you could use `exec()`:
152152
153153
```Python
154-
heroes = await session.exec(select(Hero)).all()
154+
heroes = (await session.exec(select(Hero))).all()
155155
```
156156
"""
157157
return await super().execute(

0 commit comments

Comments
 (0)