Introducing Flask-Migrate - Database Migration Management in Flask


Written on October 8, 2024

Flask-Migrate is a powerful extension for Flask that simplifies database migration management. Built on top of Alembic, a database versioning tool for SQLAlchemy, Flask-Migrate allows you to perform operations like adding, modifying, and deleting tables in your database without directly interacting with it.

Installing Flask-Migrate

To get started with Flask-Migrate, you need to install it alongside Flask and SQLAlchemy. You can install it using pip:

pip install Flask Flask-SQLAlchemy Flask-Migrate psycopg2

Configuring Flask-Migrate

  1. Create Flask app
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://username:password@localhost/dbname'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)
migrate = Migrate(app, db)

  1. Create model user
    class User(db.Model):
     id = db.Column(db.Integer, primary_key=True)
     username = db.Column(db.String(80), unique=True, nullable=False)
     email = db.Column(db.String(120), unique=True, nullable=False)
    
  2. Initialize Migration
flask db init

Generate new migration files

flask db migrate -m "Add user model"

Then apply to database

flask db upgrade