Skip to the content.

SQL Database: SQLModel + Alembic

Recipes (PostgreSQL)

uv add psycopg[binary] sqlmodel alembic alembic-postgresql-enum
uv run alembic init --template pyproject alembic
# alembic/env.py

from sqlmodel import SQLModel
import alembic_postgresql_enum
from app.db_models import XXXModel

target_metadata = SQLModel.metadata
# alembic/script.py.mako

import sqlmodel
uv run alembic check  # check migrations

uv run alembic revision --autogenerate -m "Initial migration."  # generate a new migration

uv run alembic upgrade head  # apply all migrations

uv run alembic downgrade -1  # rollback to the previous migration

uv run alembic history  # show migration history

uv run alembic current  # show the current revision

References