Installing the Flask-migrate plugin
(venv) $ pip Install Flask-migrate
Notice in the virtual environment (because the flask environment is installed in the virtual environment)
Installing Flask-script enables Python to support command-line operations
Pip Install Flask-script
Create a manage.py file
#!/usr/bin/env pythonfrom flask_script Import managerfrom flask_migrate import migrate,migratecommand# Import app initialization in main file managefrom zhihu import app#db = SQLAlchemy () from exts import db# importing the database model that needs to be migrated from models import user,question s,answer# let Python support command line work Manager = Manager (APP) #使用migrate绑定app和dbmigrate = Migrate (app,db) # Add a Migration script command to the manager in Manager.add_command (' db ', migratecommand) if __name__ = = ' __main__ ': manager.run ()
Initialization
(venv) $ Python manage.py db init
Create a migration script
(venv) $ Python manage.py db migrate
Update Database
(venv) $ python hello.py db upgrade
The first migration is actually the equivalent of calling Db.create_all (), but in subsequent migrations, the upgrade command implements the update operation on the table without affecting the contents of the table.
Finally, see if the data table is migrated successfully to the database.
migrating databases using the flask-migrate extension in flask