Flask-migrate modification of models model in midstream

Source: Internet
Author: User
Tags rollback

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 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.