安装
pip install Flask-Migrate
安装依赖
pip install Flask-Script Flask-SQLAlchemy
代码示例
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()
# 1、初始化 python main.py db init # 2、生成迁移文件 python main.py db migrate -m "备注信息" # 3、执行升级 python main.py db upgrade # 4、查看历史记录 python main.py db history # 5、执行降级 python main.py db downgrade <版本号>
mac上安装报错
brew install pandoc OSError: No pandoc was found
莫名的依赖…无解