Overview
Migrations are stored in themigrations/ directory and track changes to your database schema over time. Flask-Migrate provides CLI commands to create, apply, and manage migrations.
Migration Directory Structure
Configuration
Flask-Migrate is initialized inapp/__init__.py:
app/__init__.py:201
The
render_as_batch=True option enables batch mode for SQLite, which is required for certain schema alterations.Common Migration Commands
Applying Migrations
Upgrade to latest schema
Apply all pending migrations to bring your database up to date:This is the most common command and should be run:
- After pulling code with new migrations
- During initial production deployment
- As part of your deployment pipeline
Creating New Migrations
Auto-generate migration
After modifying SQLAlchemy models, generate a migration automatically:This detects schema changes and creates a new migration file in
migrations/versions/.Review generated migration
Open the generated file in
migrations/versions/ and verify:- Table and column names are correct
- Data types are appropriate
- Indexes and constraints are preserved
- No unintended drops or alterations
Rolling Back Migrations
Downgrade to a previous version if needed:Manual Migrations
Create an empty migration for manual schema changes:upgrade() and downgrade() logic.
Migration Workflows
Development Workflow
Production Deployment Workflow
Apply migrations
Database Initialization
First-Time Setup
For a new database, initialize the migration system:The migrations directory is already included in the repository, so
flask db init is typically not needed.Seeding Initial Data
For development, use the provided seeding script:admin123.
Troubleshooting
”Target database is not up to date”
Means pending migrations exist. Run:“Can’t locate revision identified by ‘xyz’”
Migration files are out of sync. Ensure all migrations are committed to version control and pulled on all environments.Auto-detection misses changes
Some changes aren’t detected automatically:- Table or column renames (use
op.rename_table()orop.alter_column()) - Changes to column constraints
- Server defaults
SQLite limitations
SQLite doesn’t support many ALTER operations. Flask-Migrate’s batch mode works around this, but some operations may still fail. Consider using PostgreSQL for production.Best Practices
Always review auto-generated migrations
Always review auto-generated migrations
Alembic’s auto-detection is good but not perfect. Review every migration before applying to production.
Use descriptive migration messages
Use descriptive migration messages
Test migrations on a copy of production data
Test migrations on a copy of production data
Before running migrations in production, test on a recent database dump to catch issues.
Never edit applied migrations
Never edit applied migrations
Once a migration is applied (especially in production), never modify it. Create a new migration to fix issues.
Keep migrations small and focused
Keep migrations small and focused
Break large schema changes into multiple migrations for easier troubleshooting and rollback.
Environment Variables
Migration commands respect the same database configuration as the application:config.py:38-61
Next Steps
Production Deployment
Deploy your application to production
Monitoring
Set up logging and health checks