Database Migrations
Database migrations record schema changes such as creating tables, adding columns, changing indexes, and updating constraints. In Flask projects, the common stack is SQLAlchemy plus Flask-Migrate. Flask-Migrate uses Alembic under the hood, so model changes can become versioned migration scripts.
The goal is not only to change the database. The goal is to make schema changes reviewable, repeatable, and safe across development, testing, and production environments.
Install And Initialize
Bind the extension in your application:
Create the migration directory:
This creates migrations/. Commit this directory to Git because it contains the database version history for the project.
Basic Workflow
After changing your models, generate a migration:
Review the generated migration file, then apply it:
Check the current database version:
Rollback one migration:
Example: Add A Column
Suppose the original user model has only id and email:
Add a nickname:
Then run:
Always inspect the generated script. Autogeneration usually detects simple additions, but complex changes often need manual edits.
Team Workflow
A reliable team workflow is:
- Change SQLAlchemy models.
- Run
flask db migrate -m "clear message". - Review the migration script.
- Run
flask db upgradelocally. - Run tests.
- Commit model changes and migration files together.
After pulling the latest code, teammates can update their local database with:
Production Notes
- Back up the database before running migrations.
- Run the migration in a staging environment first.
- Avoid expensive schema changes during peak traffic.
- For large tables, split risky changes into phases: add nullable column, backfill data, then add a non-null constraint.
- Do not rewrite old migrations that have already been shared. Create a new migration instead.
Common Problems
migrate does not detect changes
Make sure your models are imported by the Flask application. If the model module is never loaded, Alembic cannot compare model metadata with the database schema.
The generated script wants to drop a column
Do not run it blindly. Renames are often detected as "drop old column and add new column", which can lose data. Edit the migration script to perform a rename or data-preserving operation.
Database version and code are out of sync
Inspect the current state:
If the database schema has already been manually updated and only the migration version is missing, you can carefully mark it:
stamp records a version without applying schema changes, so use it only when you are certain the database structure is already correct.
Summary
Treat migrations as part of your codebase. Commit model changes and migration scripts together, review scripts before applying them, and back up production databases before upgrades. This habit makes Flask database changes safer and easier to coordinate across a team.