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)

with app.app_context():
db.create_all()

Since we will be using Flask-Migrate to create our database, we no longer need to tell Flask-SQLAlchemy to do it when we create the app.

Delete these two lines:

with app.app_context():
db.create_all()