flask-migrate是flask的一個擴展模塊,主要是擴展數據庫表結構的.html
官方文檔:http://flask-migrate.readthedocs.io/en/latest/python
使用flask-migrate須要依賴flask-script組件,詳見flask-script詳解
sql
pip install flask-migrate
from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_script import Manager from flask_migrate import Migrate, MigrateCommand app = 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(128)) if __name__ == '__main__': manager.run()
$ python manage.py db init 初始化數據庫,會建立一個migations文件夾,而且會在數據庫中生成一個alembic_version表 $ python manage.py db migrate 建立遷移歷史 $ python manage.py db upgrade 更新數據庫
operator = db.Column(db.String(40), nullable=True, server_default="init_data")
這裏用參數server_default,而不是default數據庫
Heroku Postgres, db migrate fail: alembic.util.exc.CommandError: Can't locate revision identified by
將alembic_version表中的數據刪掉便可flask