Python编程:Flask扩展Flask-Migrate迁移数据库

版权声明:本文为博主原创文章,欢迎转载,请注明出处 https://blog.csdn.net/mouday/article/details/86701450

安装

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

莫名的依赖…无解

没有更多推荐了,返回首页