Flask-migrate Database Migration

Source: Internet
Author: User

Database migration
    • In flask, you can use the Flask-migrate extension to implement data migration. and integrated into the flask-script, all operations can be done by command.
    • To export the database Migration command, Flask-migrate provides a Migratecommand class that can be attached to the Flask-script Manager object.

    • Create a migration warehouse

Python3 database.py DB Init

    • Create a migration script

    • There are two functions to automatically create a migration script
      • Upgrade (): The function applies the changes in the migration to the database.
      • Downgrade (): The function will delete the changes.
    • The automatically created migration scripts generate the contents of the upgrade () and downgrade () functions based on the model definition and the current state of the database.
    • The contrast may not be entirely correct, there might be some missing details, need to be checked

Python3 database.py db migrate-m " initial Migration "

    • Update Database
      # Update python3 database.py DB Upgrade # View historical Versions python app.py db history #  Fallback Python3 app.py DB downgrade version number " " actual order of operation: 1.python file db init2.python file db migrate-m "version name (note)" 3.python file DB upgrade then observe table Structure 4. Modify the model according to requirements 5.python file db mi Grate-m "new name (note)" 6.python file db upgrade then watch the table structure 7. If you return to the version, use the Python file DB history to view the version number 8.python file DB downgrade (upgrade) version number  " "

1  fromFlaskImportFlask2  fromFlask_sqlalchemyImportSQLAlchemy3  fromFlask_migrateImportMigratecommand, Migrate4  fromFlask_scriptImportManager5 6 " "7 migratecommand: Migration Command8 9 First Step initialization: Python3 demo6_ Database migration. PY DB initTen The second step is to build the migration version file: Python3 demo6_ Database migration. PY db migrate-m ' initial_01 ' One The third step is to perform the migration: Python3 demo6_ Database migration. PY DB Upgrade|downgrade A  -  - " " the  -App = Flask (__name__) -  - #set the URL of the connection database +app.config['Sqlalchemy_database_uri'] ='Mysql://root:[email protected]:3306/migratetest' -app.config['sqlalchemy_track_modifications'] =True +  Adb =SQLAlchemy (APP) at  - #migration initialization, the first parameter is the Flask instance, the second parameter is the DB instance - Migrate (app, DB) - #Second, the instance Terminal command executes the object -Manager =Manager (APP) - #Iii. Adding a migration command to the manager inManager.add_command ('DB', Migratecommand) -  to  +  -  the #Creating a data table model----instantiating a record in a data table * classRole (db. Model): $     #Defining table NamesPanax Notoginseng     __tablename__='Roles' -     #Defining Column Objects theID = db. Column (db. Integer, primary_key=True) +Nike_name = db. Column (db. String (+), unique=true)#Modify Field names AAge = db. Column (db. Integer, Nullable=true)#new Field the     #height = db. Column (db. Integer) # Add field, delete this field +user = Db.relationship ('User', backref='role') -  $  $     #The repr () method displays a readable string -     def __repr__(self): -         return 'role:%s'%Self.name the  - Wuyi classUser (db. Model): the     __tablename__='Users' -ID = db. Column (db. Integer, primary_key=True) WuName = db. Column (db. String (+), Unique=true, index=True) -email = db. Column (db. String (unique=),True) AboutPassword = db. Column (db. String (64)) $role_id = db. Column (db. Integer, Db. ForeignKey ('roles.id')) -  -     def __repr__(self): -         return 'user:%s'%Self.name A  +  the@app. Route ('/') - defindex (): $     return "Hello World" the  the  the  the if __name__=='__main__': - Manager.run () in     

Flask-migrate Database Migration

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.