Database migrations help you change your database structure safely as your app grows. Flask-Migrate makes this easy by tracking and applying these changes step-by-step.
Database migrations with Flask-Migrate
from flask_migrate import Migrate migrate = Migrate(app, db) # Then use command line: # flask db init # flask db migrate -m "message" # flask db upgrade
Flask-Migrate works with Flask and SQLAlchemy to manage database changes.
Use the command line commands to create and apply migrations.
flask db init
flask db migrate -m "Add user email column"flask db upgrade
This Flask app sets up a User model and uses Flask-Migrate to handle database changes. You run commands to create migration files and update the database.
from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app) migrate = Migrate(app, db) class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), unique=True, nullable=False) # After saving this, run in terminal: # flask db init # flask db migrate -m "Create user table" # flask db upgrade if __name__ == '__main__': app.run(debug=True)
Always review migration scripts before applying them to avoid unwanted changes.
Keep your migration files in version control to share with your team.
Use descriptive messages with flask db migrate -m "message" to remember what each migration does.
Flask-Migrate helps manage database changes safely and easily.
Use command line commands to create and apply migrations.
Keep your database schema organized as your app grows.
Practice
Solution
Step 1: Understand Flask-Migrate's role
Flask-Migrate is a tool that helps manage changes to the database structure over time.Step 2: Identify the correct purpose
It uses migrations to safely update the database schema without losing data.Final Answer:
To manage database schema changes safely using migrations -> Option CQuick Check:
Flask-Migrate = database migrations [OK]
- Confusing Flask-Migrate with template or static file handling
- Thinking it manages user sessions
- Assuming it creates HTML or CSS files
Solution
Step 1: Recall the initialization command
The command to set up Flask-Migrate for the first time isflask db init.Step 2: Check other options
Other commands likeflask migrate startorflask db createare incorrect or do not exist.Final Answer:
flask db init -> Option AQuick Check:
Initialize Flask-Migrate = flask db init [OK]
- Using 'flask migrate init' instead of 'flask db init'
- Confusing 'start' with 'init'
- Trying commands that don't exist in Flask-Migrate
flask db init flask db migrate -m "Add users table" flask db upgrade
What is the effect on the database?
Solution
Step 1: Understand each command's role
flask db initsets up migration files,flask db migratecreates a migration script, andflask db upgradeapplies changes to the database.Step 2: Determine the final effect
After running all three, the database schema is updated with the new users table as described in the migration.Final Answer:
The database schema is updated with the new users table -> Option DQuick Check:
Upgrade applies migration = schema updated [OK]
- Thinking upgrade resets the database
- Believing migrate alone applies changes
- Assuming changes need app restart
flask db migrate but see no new migration script created. What is the most likely cause?Solution
Step 1: Understand what triggers migration scripts
flask db migratecreates a script only if it detects changes in the database models.Step 2: Identify the cause of no new script
If no changes are found, no new migration script is generated, which is the most common reason.Final Answer:
No changes detected in the models since last migration -> Option BQuick Check:
No model changes = no migration script [OK]
- Assuming database server status affects migration script creation
- Thinking app restart is needed for migrations
- Forgetting to initialize migrations (less common if already done)
Solution
Step 1: Change the model to reflect the new column name
Modify your SQLAlchemy model to rename the column as desired.Step 2: Create and apply migration
Runflask db migrateto generate the migration script, thenflask db upgradeto apply it to the database.Final Answer:
Update the model's column name, run flask db migrate, then flask db upgrade -> Option AQuick Check:
Model change + migrate + upgrade = rename column [OK]
- Running upgrade before migrate
- Manually deleting columns instead of migrating
- Restarting app without migration commands
Ready to prove it?
Pass the test and this pattern moves unknown
