Python/SQLAlchemy migration - ValueError: need more than 3 values to unpack when migrating

Python/SQLAlchemy migration - ValueError: need more than 3 values to unpack when migrating

我正在关注 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 是字符串表示形式。但是我想把它变成一个外键关系,这样我就可以很容易地按天搜索和排序,所以我在模型中添加了以下代码:

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

然后修改了 类 模型,将日列更改为:

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'.

为什么我改了数据库里的字段类型就卡住了?

是的,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

[1] https://github.com/stackforge/sqlalchemy-migrate/blob/75034abe515a49e0efafb91519cfdf448ba02c73/migrate/versioning/schemadiff.py#L121