Database Schema Migrations with Flask-Migrate0%
Password Hashing with Werkzeug Security

Database Schema Migrations with Flask-Migrate

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

Database Schema Migrations with Flask-Migrate

In development, dropping and recreating database tables with db.drop_all() and db.create_all() wipes out all existing data. In production, destroying production data to add a new column or table is completely unacceptable.

Flask-Migrate wraps Alembic—SQLAlchemy's database migration engine—into the flask CLI, providing automated schema diffing and version-controlled migration scripts.


1. Installing and Configuring Flask-Migrate

Install the extension:

Bash / Terminal
pip install flask-migrate

In app.py:

Python
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
 
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///instance/app.db"
 
db = SQLAlchemy(app)
# Initialize Migrate with app and db instances
migrate = Migrate(app, db)

2. The Migration Lifecycle

Flask-Migrate adds the flask db command group to your terminal:

Visual Architecture & Process Flow

How data and code flow step-by-step

Flowchart
Step 1
1. flask db migrate -m "Add avatar column"
Step 2
Generates migration script in migrations/versions/

Step 1: Initializing Migrations Repository

Run this once when starting a new project:

Bash / Terminal
flask db init

This creates a migrations/ directory in your project root containing Alembic configuration files and an alembic_version tracking table.

Step 2: Generating a Migration Script

Whenever you add, modify, or delete columns or models in models.py, generate a revision:

Bash / Terminal
flask db migrate -m "Add bio and social_links to User"

Alembic inspects your SQLAlchemy models, compares them to the active database schema, and writes a Python migration script into migrations/versions/.

Step 3: Inspecting the Generated Script

Always inspect the generated file in migrations/versions/. Notice the upgrade() and downgrade() functions:

Python
# migrations/versions/4a2b1c8f_add_bio.py
def upgrade():
op.add_column('users', sa.Column('bio', sa.Text(), nullable=True))
 
def downgrade():
op.drop_column('users', 'bio')

Step 4: Applying Migrations

Apply the revision to your database:

Bash / Terminal
flask db upgrade

Step 5: Rolling Back a Migration

If a migration causes an issue, roll back to the previous revision:

Bash / Terminal
flask db downgrade

3. Important Caveats & SQLite Limitations

  1. 1
    SQLite Alter Column Restrictions: SQLite does not natively support dropping or altering columns. In Flask-Migrate, enable render_as_batch=True in Migrate(app, db, render_as_batch=True) so Alembic recreates the table under the hood during complex migrations on SQLite.
  2. 2
    Commit Migrations to Git: The migrations/ directory must be checked into version control so team members and CI/CD deployment pipelines apply identical schema migrations.

Interactive Quiz

Practice Quiz

Question 1
What underlying library powers Flask-Migrate?
Question 2
Which command is run once per project to establish the migrations tracking repository?
Question 3
What command automatically compares your SQLAlchemy models against the current database schema and creates a revision script?
Question 4
What command executes pending migration scripts and updates the database schema?
Question 5
Why should the migrations/ directory be committed to Git?
Next Lesson

Password Hashing with Werkzeug Security

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext