【发布时间】:2019-12-09 15:35:07
【问题描述】:
我正在使用 Flask 中的 MySQL 数据库,我需要更新模型。我已准备好迁移以创建新表列和使用 current 主键更新新列的脚本。然后用 new 主键重新插入该行。
有没有办法将重新分配脚本与migrate 命令结合起来?还是在模型更新后手动运行更好?
model.py
class Outcome(db.Model):
id = db.Column(db.Integer, primary_key=True)
outcome_id = db.Column(db.Integer) # the new property, should match id
title = db.Column(db.String(64))
course_id = db.Column(db.Integer)
assignment_id = db.relationship('Assignment', uselist=False, back_populates='outcome')
migration.py
from app import db
from app.models import Outcome
from sqlalchemy.orm.session import make_transient
outcomes = Outcome.query.all()
for outcome in outcomes:
db.session.expunge(outcome) # use only the object
make_transient(outcome)
outcome.outcome_id = outcome.id #add the new column value
outcome.id = None #set the ID to none to inherit a sequential ID
db.session.add(outcome)
db.session.commit()
【问题讨论】:
标签: python flask-sqlalchemy flask-migrate