![Flask Framework Cookbook(Second Edition)](https://wfqqreader-1252317822.image.myqcloud.com/cover/513/36698513/b_36698513.jpg)
上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:
![](https://epubservercos.yuewen.com/71C70F/19470377708803606/epubprivate/OEBPS/Images/8ee80ce6-cdce-4e39-a8bd-0ca13eca8ddf.png?sign=1738843068-qLO3t3dgy7dbsEFS9M7HJTPUlnlGgpnE-0-2fc24c2ad50d1d226588eee3f70d82c7)
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