Last modified: Jun 11, 2025 By Alexander Williams

How to Install Flask-Migrate in Python

Flask-Migrate is a Flask extension for handling database migrations. It simplifies database changes in Flask applications. This guide will help you install and use it.

What is Flask-Migrate?

Flask-Migrate is built on Alembic. It provides database migration tools for Flask apps using SQLAlchemy. It helps manage schema changes easily.

Migrations are essential for evolving databases. They ensure smooth updates without data loss. Flask-Migrate makes this process seamless.

Prerequisites

Before installing Flask-Migrate, ensure you have:

  • Python 3.6 or higher
  • Flask installed
  • SQLAlchemy installed
  • A Flask application setup

If you need help with Flask setup, check our other guides like Install Flask-Mail in Python.

Install Flask-Migrate

Use pip to install Flask-Migrate. Run this command in your terminal:


pip install Flask-Migrate

This will install Flask-Migrate and its dependencies. Verify the installation with:


pip show Flask-Migrate

Setting Up Flask-Migrate

After installation, integrate Flask-Migrate into your Flask app. Here's a basic setup:


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'
db = SQLAlchemy(app)
migrate = Migrate(app, db)

The Migrate class connects your app with the database. It enables migration commands.

Creating Migrations

First, initialize the migration repository. Run this command:


flask db init

This creates a 'migrations' folder. It stores all migration scripts. Next, create your first migration:


flask db migrate -m "Initial migration"

The migrate command generates a new migration script. Review it before applying.

Applying Migrations

To apply the migration to your database, run:


flask db upgrade

This executes all pending migrations. Your database schema is now updated. Check the status with:


flask db current

Example Model and Migration

Here's a complete example. First, define a simple User model:


class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), nullable=False)
    email = db.Column(db.String(120), unique=True)

    def __repr__(self):
        return f'<User {self.name}>'

After defining the model, create and apply the migration:


flask db migrate -m "Add User model"
flask db upgrade

Rolling Back Migrations

To undo a migration, use the downgrade command:


flask db downgrade

This reverts the last applied migration. Specify a target revision if needed.

Common Issues

If you face errors, check these solutions:

  • Ensure Flask app is properly configured
  • Verify database connection settings
  • Check for circular imports in models

For other Python package installations, see How to Install pytz in Python.

Conclusion

Flask-Migrate simplifies database management in Flask applications. It provides essential tools for schema migrations. Follow this guide to integrate it into your projects.

For more Python guides, check our Install pycountry in Python tutorial.