SQLAlchemy database migrations for Flask applications using Alembic
  • Python 87.4%
  • Mako 12.6%
Find a file
Miguel Grinberg 0d3cc1cba5
All checks were successful
build / lint (push) Successful in 1m40s
build / tests-3.10 sqlalchemy<2 flask-sqlalchemy<3.1 (push) Successful in 1m43s
build / tests-3.10 sqlalchemy>=2 flask-sqlalchemy>=3.1 (push) Successful in 1m46s
build / tests-3.11 sqlalchemy<2 flask-sqlalchemy<3.1 (push) Successful in 1m36s
build / tests-3.11 sqlalchemy>=2 flask-sqlalchemy>=3.1 (push) Successful in 1m39s
build / tests-3.12 sqlalchemy<2 flask-sqlalchemy<3.1 (push) Successful in 1m48s
build / tests-3.12 sqlalchemy>=2 flask-sqlalchemy>=3.1 (push) Successful in 1m44s
build / tests-3.13 sqlalchemy<2 flask-sqlalchemy<3.1 (push) Successful in 1m54s
build / tests-3.13 sqlalchemy>=2 flask-sqlalchemy>=3.1 (push) Successful in 1m43s
build / zizmor (push) Successful in 8s
build / tests-3.14 sqlalchemy<2 flask-sqlalchemy<3.1 (push) Successful in 1m59s
build / tests-3.14 sqlalchemy>=2 flask-sqlalchemy>=3.1 (push) Successful in 1m55s
Update project URLs in metadata
2026-08-29 23:53:06 +01:00
.forgejo/workflows Forgejo migration 2026-08-29 20:30:46 +01:00
docs Accept arguments such as --directory via environment variables (Fixes #553) 2025-01-07 00:25:14 +00:00
src/flask_migrate Accept arguments such as --directory via environment variables (Fixes #553) 2025-01-07 00:25:14 +00:00
tests Allow process_revision_directives option to be configurable (#523) 2023-07-21 11:18:35 +01:00
.gitignore Update project URLs in metadata 2026-08-29 23:53:06 +01:00
.readthedocs.yaml Forgejo migration 2026-08-29 20:30:46 +01:00
CHANGES.md Release 4.1.0 2025-01-10 18:50:24 +00:00
CODE_OF_CONDUCT.md Forgejo migration 2026-08-29 20:30:46 +01:00
CONTRIBUTING.md Forgejo migration 2026-08-29 20:30:46 +01:00
LICENSE Initial commit 2013-09-08 12:24:57 -07:00
MANIFEST.in Migrate Python package metadata to pyproject.toml 2023-10-15 12:54:41 +01:00
pyproject.toml Update project URLs in metadata 2026-08-29 23:53:06 +01:00
README.md Forgejo migration 2026-08-29 20:30:46 +01:00
SECURITY.md Forgejo migration 2026-08-29 20:30:46 +01:00
tox.ini tox configuration 2026-05-14 20:42:27 +01:00

Flask-Migrate

tests

Flask-Migrate is an extension that handles SQLAlchemy database migrations for Flask applications using Alembic. The database operations are provided as command-line arguments under the flask db command.

Installation

Install Flask-Migrate with pip:

pip install Flask-Migrate

Example

This is an example application that handles database migrations through Flask-Migrate:

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)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(128))

With the above application you can create the database or enable migrations if the database already exists with the following command:

$ flask db init

Note that the FLASK_APP environment variable must be set according to the Flask documentation for this command to work. This will add a migrations folder to your application. The contents of this folder need to be added to version control along with your other source files.

You can then generate an initial migration:

$ flask db migrate

The migration script needs to be reviewed and edited, as Alembic currently does not detect every change you make to your models. In particular, Alembic is currently unable to detect indexes. Once finalized, the migration script also needs to be added to version control.

Then you can apply the migration to the database:

$ flask db upgrade

Then each time the database models change repeat the migrate and upgrade commands.

To sync the database in another system just refresh the migrations folder from source control and run the upgrade command.

To see all the commands that are available run this command:

$ flask db --help

Resources

Sponsor this project

This project relies on contributions from its users. If you benefit from it please consider making a single or ongoing monetary contribution in one of the following platforms:

Thank you!