
上QQ阅读APP看书,第一时间看更新
How to do it...
To enable migrations, we need to modify our app definition a bit. Let's understand how such a config appears if we modify the same for our catalog application:
The following lines of code show how my_app/__init__.py appears:
from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate, MigrateCommand app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db' db = SQLAlchemy(app) migrate = Migrate(app, db) import my_app.catalog.views db.create_all()
If we pass --help to the flask command while running it as a script, the Terminal will show all the available options, as shown in the following screenshot:

To initialize migrations, run the init command:
$ flask db init
The preceding command will not work if you do not have the FLASK_APP environment variable configured. This can simply be done by running the following command on the Terminal:
$ export FLASK_APP=my_app
$ export FLASK_APP=my_app
Once changes are made to the models, call the migrate command:
$ flask db migrate
To make the changes reflect on the database, call the upgrade command:
$ flask db upgrade