Flask-Migrate数据库迁移组件

文章目录

Flask-Migrate 可以做 数据库迁移,与Django中数据迁移功能相同。Flask-Migrate 依赖于 flask_script 来执行命令:

manage.py:

from app import create_app
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from app import db

app = create_app()
manager = Manager(app)
Migrate(app, db)
manager.add_command('db', MigrateCommand)
"""
数据库迁移命令:
python manage.py db init
python manage.py db migrate # 相当于Django中makemigrations
python manage.py db upgrade # 相当于Django中的migrate
"""
if __name__ == '__main__':
    manager.run()
    # app.run(port=5051)

在项目目录下执行数据库迁移命令:

$ python manage.py db init
$ python manage.py db migrate
$ python manage.py db upgrade

执行迁移操作之前需要保证 models.py 中有写表对应的类,也要确保把导入 model.py,即 import app.models 或 from app.models import *

你可能感兴趣的:(Flask)