MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Database Schema Migrations with Flask-Migrate0%
Study Materials
FlaskProgress 0%
1.1What is Flask & WSGI Architecture1.2Virtual Environments & First Flask Application1.3Flask CLI & Environment Configuration
5.1Flask-SQLAlchemy Setup & Engine Configuration5.2Defining Models & Column Constraints5.3Database Relationships (One-to-Many & Many-to-Many)5.4CRUD Operations, Query Filtering & Pagination5.5Database Schema Migrations with Flask-Migrate
HomeTutorialsFlaskDatabase Schema Migrations with Flask-Migrate
Password Hashing with Werkzeug Security

Database Schema Migrations with Flask-Migrate

Beginner12 min read•Updated: 2026-09-16•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

In development, dropping and recreating database tables with db.drop_all() and db.create_all() wipes out all existing data. In production, destroying production data to add a new column or table is completely unacceptable.

Flask-Migrate wraps Alembic—SQLAlchemy's database migration engine—into the flask CLI, providing automated schema diffing and version-controlled migration scripts.


1. Installing and Configuring Flask-Migrate

Install the extension:

1pip install flask-migrate

In app.py:

1from flask import Flask
2from flask_sqlalchemy import SQLAlchemy
3from flask_migrate import Migrate
4 
5app = Flask(__name__)
6app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///instance/app.db"
7 
8db = SQLAlchemy(app)
9# Initialize Migrate with app and db instances
10migrate = Migrate(app, db)

2. The Migration Lifecycle

Flask-Migrate adds the flask db command group to your terminal:

Visual Architecture & Process Flow

How data and code flow step-by-step

Flowchart
Step 1
1. flask db migrate -m "Add avatar column"
Step 2
Generates migration script in migrations/versions/

Step 1: Initializing Migrations Repository

Run this once when starting a new project:

1flask db init

This creates a migrations/ directory in your project root containing Alembic configuration files and an alembic_version tracking table.

Step 2: Generating a Migration Script

Whenever you add, modify, or delete columns or models in models.py, generate a revision:

1flask db migrate -m "Add bio and social_links to User"

Alembic inspects your SQLAlchemy models, compares them to the active database schema, and writes a Python migration script into migrations/versions/.

Step 3: Inspecting the Generated Script

Always inspect the generated file in migrations/versions/. Notice the upgrade() and downgrade() functions:

1# migrations/versions/4a2b1c8f_add_bio.py
2def upgrade():
3 op.add_column('users', sa.Column('bio', sa.Text(), nullable=True))
4 
5def downgrade():
6 op.drop_column('users', 'bio')

Step 4: Applying Migrations

Apply the revision to your database:

1flask db upgrade

Step 5: Rolling Back a Migration

If a migration causes an issue, roll back to the previous revision:

1flask db downgrade

3. Important Caveats & SQLite Limitations

  1. 1
    SQLite Alter Column Restrictions: SQLite does not natively support dropping or altering columns. In Flask-Migrate, enable render_as_batch=True in Migrate(app, db, render_as_batch=True) so Alembic recreates the table under the hood during complex migrations on SQLite.
  2. 2
    Commit Migrations to Git: The migrations/ directory must be checked into version control so team members and CI/CD deployment pipelines apply identical schema migrations.

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What underlying library powers Flask-Migrate?
Question 2
Which command is run once per project to establish the migrations tracking repository?
Question 3
What command automatically compares your SQLAlchemy models against the current database schema and creates a revision script?
Question 4
What command executes pending migration scripts and updates the database schema?
Question 5
Why should the migrations/ directory be committed to Git?
Next Lesson

Password Hashing with Werkzeug Security

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
CRUD Operations, Query Filtering & PaginationPassword Hashing with Werkzeug Security

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master Flask with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext