Skip to content

Commit 52ef604

Browse files
docs: Address review feedback on async session tutorial
1 parent 88f8a55 commit 52ef604

File tree

1 file changed

+34
-8
lines changed
  • docs/en/docs/tutorial/async-session

1 file changed

+34
-8
lines changed

docs/en/docs/tutorial/async-session/index.md

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class Hero(SQLModel, table=True):
1818
age: int | None = Field(default=None, index=True)
1919

2020
# Note the +asyncpg in the database URL
21-
sqlite_url = "postgresql+asyncpg://user:password@localhost/db"
22-
engine = create_async_engine(sqlite_url, echo=True)
21+
async_url = "postgresql+asyncpg://user:password@localhost/db"
22+
engine = create_async_engine(async_url, echo=True)
2323

2424
async def create_db_and_tables():
2525
async with engine.begin() as conn:
@@ -28,10 +28,14 @@ async def create_db_and_tables():
2828
async def create_heroes():
2929
hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson")
3030
async with AsyncSession(engine) as session:
31-
session.add(hero_1)
32-
await session.commit()
33-
await session.refresh(hero_1)
34-
print("Created hero:", hero_1)
31+
try:
32+
session.add(hero_1)
33+
await session.commit()
34+
await session.refresh(hero_1)
35+
print("Created hero:", hero_1)
36+
except Exception as e:
37+
await session.rollback()
38+
print("Failed to create hero:", e)
3539

3640
async def read_heroes():
3741
async with AsyncSession(engine) as session:
@@ -43,11 +47,22 @@ async def read_heroes():
4347

4448
## Using with FastAPI
4549

46-
When integrating with FastAPI, you can use `AsyncSession` as a dependency.
50+
When integrating with FastAPI, you can use `AsyncSession` as a dependency. Note that you should explicitly handle rollbacks when performing write operations.
4751

4852
```python
49-
from fastapi import FastAPI, Depends
53+
from fastapi import FastAPI, Depends, HTTPException
5054
from sqlmodel.ext.asyncio.session import AsyncSession
55+
from sqlmodel import SQLModel, Field, select
56+
from sqlalchemy.ext.asyncio import create_async_engine
57+
58+
class Hero(SQLModel, table=True):
59+
id: int | None = Field(default=None, primary_key=True)
60+
name: str = Field(index=True)
61+
secret_name: str
62+
age: int | None = Field(default=None, index=True)
63+
64+
async_url = "postgresql+asyncpg://user:password@localhost/db"
65+
engine = create_async_engine(async_url, echo=True)
5166

5267
app = FastAPI()
5368

@@ -61,4 +76,15 @@ async def read_heroes(session: AsyncSession = Depends(get_session)):
6176
result = await session.exec(statement)
6277
heroes = result.all()
6378
return heroes
79+
80+
@app.post("/heroes/")
81+
async def create_hero(hero: Hero, session: AsyncSession = Depends(get_session)):
82+
try:
83+
session.add(hero)
84+
await session.commit()
85+
await session.refresh(hero)
86+
return hero
87+
except Exception as e:
88+
await session.rollback()
89+
raise HTTPException(status_code=500, detail="Database error occurred")
6490
```

0 commit comments

Comments
 (0)