0
0
Flaskframework~10 mins

Database migrations with Flask-Migrate - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Database migrations with Flask-Migrate
Write/Change Models
Run 'flask db migrate'
Generate Migration Script
Review/Edit Script
Run 'flask db upgrade'
Apply Changes to DB
Database Updated
You first change your data models, then create a migration script, and finally apply it to update the database.
Execution Sample
Flask
from flask_migrate import Migrate
migrate = Migrate(app, db)

# Terminal commands:
# flask db migrate -m "Add user email"
# flask db upgrade
This sets up Flask-Migrate and runs commands to create and apply a migration.
Execution Table
StepActionCommand/CodeResult
1Change model codeAdd 'email' field to User modelModel updated in code
2Generate migrationflask db migrate -m "Add user email"Migration script created in migrations/versions
3Review migrationCheck generated script for correctnessMigration script ready
4Apply migrationflask db upgradeDatabase schema updated with new 'email' column
5VerifyCheck DB schema or app behaviorNew column available in DB
💡
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
User model fieldsid, usernameid, username, emailMigration script includes 'email' columnDatabase schema includes 'email' columnDatabase schema includes 'email' column
Key Moments - 2 Insights
Why do I need to run 'flask db migrate' after changing my models?
Because 'flask db migrate' creates a migration script that tells the database how to update its structure to match your new model changes, as shown in step 2 of the execution table.
What happens if I skip 'flask db upgrade'?
Skipping 'flask db upgrade' means the database won't apply the changes from the migration script, so your database schema stays old and mismatched with your models, as seen between steps 2 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result after running 'flask db migrate'?
ADatabase schema updated
BMigration script created in migrations/versions
CModel code changed
DMigration script applied to DB
💡 Hint
Check step 2 in the execution table for the result of 'flask db migrate'
At which step does the database schema actually get updated?
AStep 2
BStep 1
CStep 4
DStep 3
💡 Hint
Look for when 'flask db upgrade' is run in the execution table
If you forget to add the new field to your model before migrating, what happens?
AMigration script will not include the new field
BDatabase will update correctly anyway
CMigration script will fail to generate
DDatabase schema will update with extra fields
💡 Hint
Refer to the variable tracker and step 1 where model changes happen before migration
Concept Snapshot
Flask-Migrate helps update your database schema when models change.
1. Change your models in code.
2. Run 'flask db migrate' to create migration scripts.
3. Review scripts if needed.
4. Run 'flask db upgrade' to apply changes to the database.
Always migrate before upgrading to keep DB and models in sync.
Full Transcript
Database migrations with Flask-Migrate involve changing your data models first, then generating a migration script using 'flask db migrate'. This script describes how to update the database schema. After reviewing the script, you apply the changes with 'flask db upgrade', which updates the actual database. This process keeps your database structure in sync with your application models. Skipping migration or upgrade steps can cause mismatches. The execution table shows each step and its result, while the variable tracker follows the model and database schema changes. Remember to always migrate after model changes and upgrade to apply them.