The problem with writing code is that when I started designing the database models, the code was half done. I want to add a new field to a form, this time I need to use the data migration flask-migrate,flask_script.
1. Configuring the Environment in code
This is the boot entry of my manager.py program, which needs to be started by using the Flask_script command in the program Entry Manager.run (), and comment out the App.run ()
From flask_script import Manager #flask script from
flask_migrate import migrate,migratecommand #flask migrating data from
app I Mport create_app,db
app = Create_app ()
migrate = Migrate (app,db) #传入2个对象一个是flask的app对象, one is SQLAlchemy
Manager = Manager (APP)
Manager.add_command (' db ', Migratecommand) #给manager添加一个db命令并且传入一个MigrateCommand的类
@ Manager.command
def Dev (): From
livereload import server
live_server = Server (App.wsgi_app)
live_ Server.watch (' **/*.* ')
live_server.serve (open_url=true)
if __name__ = ' __main__ ':
#app. Run ()
Manager.run ()
# dev ()
Initialization of 2.flask-migrate
#执行命令
python manager.py db init
You will see that the following hints indicate successful initialization, at which point you will see an extra Migrations folder for your project
3. Change your database to
I have added a tag field to this new
Enter command after change
Python manager.py db migrate-m "Initial migration"
At this point the output of the information we have changed the field,migrations\versions will add a py migration script file, check the table and fields.
3. Updating the database
Update Database
Python manager.py DB Upgrade
And then you'll find that your fields are incremented and the database won't complain.
Database Rollback
# get History ID
python manager.py db History
# Rollback to a History
python manager.py db downgrade