Menu

Alembic migrations with async SQLAlchemy in FastAPI
📰

Alembic migrations with async SQLAlchemy in FastAPI

DEV Community: fastapi·patrykkomow12·7 days ago
#t5nbmTSC
#devtofastapi#devto#dev#alembic#import
Reading 0:00
15s threshold

Alembic itself is synchronous, your app's engine is async, and the default template doesn't know your settings or your models. Here is a setup that runs migrations through the same async engine and settings as the app, generates predictable files, and gets tested on every test run.

The engine and the declarative base

Everything starts in one module that the app, Alembic and the tests all import:

# app/db.py from sqlalchemy import MetaData from sqlalchemy.ext.asyncio import AsyncSession , async_sessionmaker , create_async_engine from sqlalchemy.orm import DeclarativeBase from sqlalchemy.pool import NullPool

# Stable constraint names, so Alembic autogenerate produces predictable migrations.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.