使用Falsk-Migrate实现数据库迁移
配置Flask-Migrate
- 注意需要用到Flask-Script中的manager命令行扩展脚本
hello.py
from flask_migrate import Migrate,MigrateCommand
# ... #导入你的数据库模型,配置数据库对象
migrate = Migrate(app,db)
manager.add_command('db',MigrateCommand)
部分命令行指令
- 创建迁移仓库
python hello.py db init
- 自动创建迁移脚本(有时候upgrade不生效,必须执行这一步)
python hello.py db migrate -m 'initial migration'
- 检查修正好迁移脚本后,更新数据库(downgrade代表删除)
python hello.py db upgrade
flask web开发坑
在Flask web开发书中创建完虚拟数据后,此时要关联User与Role的联系,有个User权限的功能需要运行
>python manager.py shell
>>>Role.insert_roles()