SQLAlchemy database migrations for Flask applications using Alembic.
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.
Install Flask-Migrate with
pip install Flask-Migrate
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
To sync the database in another system just refresh the
migrations folder from source control and run the
To see all the commands that are available run this command:
$ flask db --help
Release history Release notifications | RSS feed
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size Flask_Migrate-3.1.0-py3-none-any.whl (20.3 kB)||File type Wheel||Python version py3||Upload date||Hashes View|
|Filename, size Flask-Migrate-3.1.0.tar.gz (12.7 kB)||File type Source||Python version None||Upload date||Hashes View|
Hashes for Flask_Migrate-3.1.0-py3-none-any.whl