Flask's Flask-migrate Database migration

Source: Internet
Author: User
Tags heroku postgres

Flask-migrate is an extension module of flask, which mainly extends the structure of database tables.

Official Document: http://flask-migrate.readthedocs.io/en/latest/

Use flask-migrate need to rely on flask-script components, see my other blog--Flask-script detailed

Installation
Pip Install Flask-migrate
Creating commands with examples
From flask import flaskfrom flask_sqlalchemy import sqlalchemyfrom flask_script import managerfrom flask_migrate import Mi Grate, Migratecommandapp = Flask (__name__) app.config[' sqlalchemy_database_uri '] = ' sqlite:///app.db ' db = SQLALCHEMY ( APP) Migrate = Migrate (app, db) Manager = Manager (APP) Manager.add_command (' db ', Migratecommand) class User (db). Model):    ID = db. Column (db. Integer, primary_key=true)    name = db. Column (db. String (*)) if __name__ = = ' __main__ ':    manager.run ()
Execute command
$ python manage.py db init   initializes the database, creates a Migations folder, and generates a Alembic_version table in the database $ Python manage.py db migrate  Create migration History $ python manage.py db upgrade  update database
Pit 1. Default values for fields when designing tables
operator = db. Column (db. String (nullable=true), server_default= "Init_data")

Here is the parameter Server_default instead of the default

2. If you report this error
Heroku Postgres, db migrate Fail:alembic.util.exc.CommandError:Can ' t locate revision identified by

Delete the data from the Alembic_version table

Flask's 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.