Alembic

1.18.4 · active · verified Tue Mar 24

Database migration tool for SQLAlchemy. Manages schema versioning via migration scripts. Current version: 1.18.4 (Mar 2026). Tightly coupled to SQLAlchemy — version mismatch causes silent failures. SQLAlchemy 1.3 dropped in Alembic 1.15. Python 3.8 dropped in Alembic 1.15. The #1 footgun: autogenerate generates empty migrations when target_metadata is not correctly set in env.py.

Warnings

Install

Imports

Quickstart

Alembic setup and first migration workflow.

# 1. Install and init
# pip install alembic sqlalchemy
# alembic init alembic

# 2. Edit alembic/env.py — add your models:
# from myapp.models import Base
# target_metadata = Base.metadata

# 3. Edit alembic.ini — set database URL:
# sqlalchemy.url = postgresql://user:pass@localhost/mydb

# 4. Generate first migration
# alembic revision --autogenerate -m 'initial schema'

# 5. Apply migration
# alembic upgrade head

# Migration file (alembic/versions/xxx_initial_schema.py):
from alembic import op
import sqlalchemy as sa

def upgrade():
    op.create_table(
        'users',
        sa.Column('id', sa.Integer, primary_key=True),
        sa.Column('username', sa.String(50), nullable=False),
        sa.Column('email', sa.String(120), nullable=False),
    )

def downgrade():
    op.drop_table('users')

view raw JSON →