创建迁移仓库:
1.安装工具:
pip install flask_migarte
2.代码:
#encoding:utf-8
from flask_sqlalchemy import SQLAlchemy
from flask_script import Manager,Shell
from flask_migrate import Migrate,MigrateCommand
from flask import Flask
app = Flask(__name__)
db = SQLAlchemy(app)
migrate = Migrate(app,db)
manager = Manager(app)
manager.add_command('db',MigrateCommand)
def make_shell_context():
return dict(app=app,db=db)
if __name__=='__main__':
manager.run()
3.执行:
python index.py db init
4.效果:
5.己经创建了迁移仓库,我们需要创建一个迁移对象,把它放在仓库中,用来以后更新和恢复:
迁移角本:
python index.py db migrate -m "initial migration"
6.更新数据库(应用迁移角本):
python index.py db upgrade
7.回退数据库(返回上一个迁移角本):
python index.py db downgrade