gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:07:01 24 4
gpt4 key购买 nike

我正在关注 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

关于Python/SQLAlchemy 迁移 - ValueError : need more than 3 values to unpack when migrating,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27894702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com