📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Flask Web Framework Flask Migrations

Flask Migrations

5 min read Quiz at the end
Manage schema changes with Flask-Migrate (Alembic): init, migrate, upgrade, and downgrade.

Database Migrations with Flask-Migrate

pip install flask-migrate

from flask_migrate import Migrate
migrate = Migrate()

# In factory
migrate.init_app(app, db)

# Commands
flask db init       # create migrations/ folder (once)
flask db migrate -m "add users table"  # generate migration
flask db upgrade    # apply migrations
flask db downgrade  # rollback one
flask db history    # show migration history
flask db current    # current revision

# Generated migration (migrations/versions/xxx.py)
def upgrade():
    op.create_table("users",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("email", sa.String(255), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("email"),
    )

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