我正在关注Miguel Grinberg出色的Flask Mega-Tutorial并使用他的数据库创建和迁移脚本(找到here)但是在我的一个模型中更改列时遇到了问题。
旧模型是:
class Classes(db.Model):
id = db.Column(db.Integer, primary_key=True)
day = db.Column(db.String(10))
slot = db.Column(db.Integer)
enrolments = db.relationship('Enrolment', backref='class_slot', lazy='dynamic')
def __repr__(self):
return '<Classes - %r slot %d>' % (self.day, self.slot)
class Enrolment(db.Model):
id = db.Column(db.Integer, primary_key=True)
class_id = db.Column(db.Integer, db.ForeignKey('classes.id'))
studentnum = db.Column(db.String(10), index=True, unique=True)
name = db.Column(db.String(30))
flags = db.Column(db.String(100))
notes = db.Column(db.String(200))
def __repr__(self):
return '<Enrolment - (%r) %r>' % (self.name, self.studentnum)
使用classes.day作为String表示。但是我希望将它变成一个外键关系,这样我就可以轻松地按日搜索和排序,所以我将以下代码添加到模型中:
class Days(db.Model):
id = db.Column(db.Integer, primary_key=True)
day_name = db.Column(db.String(20), unique=True)
classes = db.relationship('Classes', backref='dayofweek', lazy='select')
def __repr__(self):
return '<Day - %r>' % self.day_name
然后修改了Classes模型,将day列更改为:
day = db.Column(db.Integer, db.ForeignKey('days.id'))
运行迁移脚本时,在生成具有以下错误的模型时会出现阻塞:
aaron$ ./db_migrate.py
Traceback (most recent call last):
File "./db_migrate.py", line 12, in <module>
script = api.make_update_script_for_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, tmp_module.meta, db.metadata)
File "<string>", line 2, in make_update_script_for_model
File "/Users/aaron/Dropbox/Development/put-attendance/flask/lib/python2.7/site-packages/migrate/versioning/util/__init__.py", line 90, in catch_known_errors
return f(*a, **kw)
File "<string>", line 2, in make_update_script_for_model
File "/Users/aaron/Dropbox/Development/put-attendance/flask/lib/python2.7/site-packages/migrate/versioning/util/__init__.py", line 160, in with_engine
return f(*a, **kw)
File "/Users/aaron/Dropbox/Development/put-attendance/flask/lib/python2.7/site-packages/migrate/versioning/api.py", line 321, in make_update_script_for_model
engine, oldmodel, model, repository, **opts)
File "/Users/aaron/Dropbox/Development/put-attendance/flask/lib/python2.7/site-packages/migrate/versioning/script/py.py", line 70, in make_update_script_for_model
genmodel.ModelGenerator(diff,engine).genB2AMigration()
File "/Users/aaron/Dropbox/Development/put-attendance/flask/lib/python2.7/site-packages/migrate/versioning/genmodel.py", line 223, in genB2AMigration
for modelCol, databaseCol, modelDecl, databaseDecl in td.columns_different:
ValueError: need more than 3 values to unpack
检查td.columns_different
表示列表中只有一个项目 - 字符串'day'
。
为什么它会窒息,因为我改变了数据库中的字段类型?
答案 0 :(得分:0)
是的,td.columns_different是在先前版本和当前版本之间更改的列的字典。此代码属于sqlalchemy-migrate软件包。
来自[1]的评论:
class TableDiff(object):
"""
...
..attribute:: columns_different
A dictionary containing information about columns that were
found to be different.
It maps column names to a :class:`ColDiff` objects describing the
differences found.
有关将Column类型从String迁移到Integer的解决方案,请参阅:How to alter column type from character varying to integer using sqlalchemy-migrate