我有以下型號SQLAlchemy的索賠列有特別的要求時,它不
models.py
class Rules(db.Model):
name=db.Column(db.String, primary_key=True)
rule=db.Column(db.Integer, default='0', unique=False)
def __repr__(self): # pragma: no cover
return '<Rules %r>' % (self.name)
如果我試圖和它存儲數據,它聲稱規則有着獨特的要求。我的事件竟然明確地聲明unique =假,但它仍然宣稱它必須與以下錯誤
sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint failed: rules.rule [SQL: u'INSERT INTO rules (name, rule) VALUES (?, ?)'] [parameters: ('dod_iaa_cyber', '2')]
我已經多次跑到下面的腳本來嘗試和遷移,並修復錯誤獨特無濟於事。
db_migrate.py
#!flask/bin/python
import imp
from migrate.versioning import api
from app import db
from config import SQLALCHEMY_DATABASE_URI
from config import SQLALCHEMY_MIGRATE_REPO
v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
migration = SQLALCHEMY_MIGRATE_REPO + ('/versions/%03d_migration.py' % (v+1))
tmp_module = imp.new_module('old_model')
old_model = api.create_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
exec old_model in tmp_module.__dict__
script = api.make_update_script_for_model(SQLALCHEMY_DATABASE_URI,
SQLALCHEMY_MIGRATE_REPO,
tmp_module.meta, db.metadata)
open(migration, "wt").write(script)
api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
print('New migration saved as ' + migration)
print('Current database version: ' + str(v))
007_migration.py,從003-007所有遷移都是一樣的,因爲這
from sqlalchemy import *
from migrate import *
from migrate.changeset import schema
pre_meta = MetaData()
post_meta = MetaData()
def upgrade(migrate_engine):
# Upgrade operations go here. Don't create your own engine; bind
# migrate_engine to your metadata
pre_meta.bind = migrate_engine
post_meta.bind = migrate_engine
def downgrade(migrate_engine):
# Operations to reverse the above upgrade go here.
pre_meta.bind = migrate_engine
post_meta.bind = migrate_engine
002_migration.py,這是一個比其他人不同的,有規則作爲主鍵
from sqlalchemy import *
from migrate import *
from migrate.changeset import schema
pre_meta = MetaData()
post_meta = MetaData()
rules = Table('rules', post_meta,
Column('rule', Integer, primary_key=True, nullable=False),
Column('name', String(length=45)),
)
def upgrade(migrate_engine):
# Upgrade operations go here. Don't create your own engine; bind
# migrate_engine to your metadata
pre_meta.bind = migrate_engine
post_meta.bind = migrate_engine
post_meta.tables['rules'].create()
def downgrade(migrate_engine):
# Operations to reverse the above upgrade go here.
pre_meta.bind = migrate_engine
post_meta.bind = migrate_engine
post_meta.tables['rules'].drop()
那麼,爲什麼新的遷移沒有回升,我改變了規則從p rimary關鍵只是一個整數,我怎樣才能正確地轉換它的未來?
生成的遷移文件有哪些內容? – univerio
良好的調用,遷移002具有作爲主鍵的規則,並且它們都沒有改變它,因爲它反映了它不是主鍵的變化。有關如何強制遷移的建議? –