Skip to main content

How to add Flask-Migrate to our Flask app

Adding Flask-Migrate to our app is simple, just install it and add a couple lines to app.py.

To install:

pip install flask-migrate

This will also install Alembic, since it is a dependency.

Then we need to add 2 lines to app.py (highlighted):

from flask_smorest import Api
from flask_migrate import Migrate

import models

app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["PROPAGATE_EXCEPTIONS"] = True
db.init_app(app)
migrate = Migrate(app, db)
api = Api(app)

@app.before_first_request
def create_tables():
db.create_all()