migrating databases using the flask-migrate extension in flask

Source: Internet
Author: User
Tags virtual environment

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

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.