我遵循Miguel Grinberg的優秀Flask Mega-Tutorial並使用他的數據庫創建和遷移腳本(找到here),但我遇到了問題當更改我的某個模型中的列時。Python/SQLAlchemy遷移 - ValueError:需要超過3個值才能在遷移時解壓
舊的模式是:
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'
。
爲什麼它窒息,因爲我改變了數據庫中的字段類型?
感謝您的回答。我不想手動編寫升級腳本。最後,我放棄了sqlalchemy-migrate,因爲它似乎與最新版本的SQLAlchemy不兼容。自那時起,我開始轉向燒瓶遷移,這很好。但我會將您的答案標記爲可接受的,因爲它提供了有用的信息和鏈接,除了「遷移」到其他解決方案之外,我並不期望修復此錯誤。 –