📖 Scenario: You are building a Flask web app that stores user data in a database. When you update your app, you need to change the database structure without losing existing data. This process is called database migration.In this project, you will set up a simple Flask app with a database, configure migration support, create a migration script, and apply the migration to update the database.
🎯 Goal: Build a Flask app that uses Flask-Migrate to handle database migrations during deployment. You will create the initial database, configure migration settings, generate a migration script, and apply it to update the database schema safely.
📋 What You'll Learn
Use Flask with SQLAlchemy for database handling
Use Flask-Migrate to manage database migrations
Create an initial User model with id and name fields
Add a new email field to the User model via migration
Apply migration commands programmatically in the app
💡 Why This Matters
🌍 Real World
Database migrations are essential when deploying updates to web apps that use databases. They help change the database structure safely without losing user data.
💼 Career
Knowing how to manage database migrations with Flask and Flask-Migrate is a key skill for backend developers working on web applications that evolve over time.
Progress0 / 4 steps
1
Set up Flask app and initial User model
Create a Flask app instance called app. Set up SQLAlchemy with db = SQLAlchemy(app). Define a User model with id as an integer primary key and name as a string column. Initialize the database with db.create_all().
Flask
Hint
Remember to import Flask and SQLAlchemy. Set the database URI to a local SQLite file. Define the User model with id and name columns. Call db.create_all() inside the app context.
2
Configure Flask-Migrate for migration support
Import Migrate from flask_migrate. Create a migrate object with migrate = Migrate(app, db) to enable migration support in your Flask app.
Flask
Hint
Import Migrate and create a migrate object passing app and db.
3
Add new email field to User model for migration
Modify the User model by adding a new column email as a string with max length 120 and allow null values. Do not remove existing fields.
Flask
Hint
Add email = db.Column(db.String(120), nullable=True) inside the User model.
4
Apply migration commands programmatically
Import upgrade from flask_migrate. Inside the app context, call upgrade() to apply the migration and update the database schema with the new email column.
Flask
Hint
Import upgrade and call it inside the app context to apply migrations.
Practice
(1/5)
1. What is the main purpose of database migration in a Flask application deployment?
easy
A. To speed up the app by removing unused tables
B. To delete all existing data before deploying
C. To safely update the database structure as the app changes
D. To backup the database automatically
Solution
Step 1: Understand database migration purpose
Database migration updates the database schema to match app changes without losing data.
Step 2: Compare options
Only To safely update the database structure as the app changes describes safe schema updates; others describe unrelated or harmful actions.
Final Answer:
To safely update the database structure as the app changes -> Option C
Quick Check:
Migration = safe schema update [OK]
Hint: Migration means changing database structure safely [OK]
Common Mistakes:
Thinking migration deletes data
Confusing migration with backup
Assuming migration speeds up app
2. Which Flask-Migrate command initializes migration scripts in your project?
easy
A. flask db migrate
B. flask db create
C. flask db upgrade
D. flask db init
Solution
Step 1: Identify command for starting migrations
The flask db init command creates the migration folder and scripts for the first time.
Step 2: Check other commands
migrate creates migration files, upgrade applies them, create is invalid.
Final Answer:
flask db init -> Option D
Quick Check:
Init = start migration scripts [OK]
Hint: Init means start or create migration setup [OK]
Common Mistakes:
Using migrate before init
Confusing upgrade with init
Typing create instead of init
3. Given these commands run in order: flask db init flask db migrate -m "Add user table" flask db upgrade What happens after the last command?
medium
A. The database schema updates with the new user table
B. The migration folder is deleted
C. The app restarts automatically
D. The migration message is ignored
Solution
Step 1: Understand each command effect
init sets up migration scripts, migrate creates migration files, upgrade applies changes to the database.
Step 2: Identify final effect
After upgrade, the database schema updates to include new tables or changes.
Final Answer:
The database schema updates with the new user table -> Option A
Quick Check:
Upgrade = apply schema changes [OK]
Hint: Upgrade applies migration changes to database [OK]
Common Mistakes:
Thinking upgrade deletes migration files
Assuming app restarts automatically
Ignoring migration message
4. You run flask db migrate but see no new migration file created. What is the likely cause?
medium
A. You forgot to run flask db init first
B. Your database is already up to date with no changes
C. You ran flask db upgrade instead
D. You need to restart the Flask app
Solution
Step 1: Understand migrate command behavior
flask db migrate creates migration files only if there are schema changes detected.
Step 2: Analyze no new file case
If no migration file appears, it means no schema changes were found, so no new migration is needed.
Final Answer:
Your database is already up to date with no changes -> Option B
Quick Check:
No changes = no migration file [OK]
Hint: No migration file means no schema changes detected [OK]
Common Mistakes:
Skipping init causes errors but not no file
Confusing upgrade with migrate
Thinking app restart affects migration files
5. You deployed a Flask app and added a new column to a model. Which sequence of commands correctly updates the production database without losing data?
hard
A. flask db migrate -m "Add column" -> flask db upgrade
B. flask db upgrade -> flask db migrate -m "Add column"
C. flask db init -> flask db upgrade
D. flask db migrate -m "Add column" -> flask db init
Solution
Step 1: Understand correct migration order
First, create migration files with flask db migrate describing changes, then apply them with flask db upgrade.
Step 2: Check options for correct sequence
flask db migrate -m "Add column" -> flask db upgrade follows this order; others reverse or misuse commands.
Final Answer:
flask db migrate -m "Add column" -> flask db upgrade -> Option A
Quick Check:
Migrate then upgrade = safe update [OK]
Hint: Always migrate before upgrade to apply changes safely [OK]
Common Mistakes:
Running upgrade before migrate
Running init after migrate
Skipping migrate step
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.