2017-05-25 28 views
0

我有以下型號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關鍵只是一個整數,我怎樣才能正確地轉換它的未來?

+1

生成的遷移文件有哪些內容? – univerio

+0

良好的調用,遷移002具有作爲主鍵的規則,並且它們都沒有改變它,因爲它反映了它不是主鍵的變化。有關如何強制遷移的建議? –

回答

0

你的名字是獨一無二的嗎?擁有主鍵意味着該字段是唯一的。

+0

名稱應該是唯一的,規則不應該。當我嘗試添加條目來規則時,它會強制它們因某種未知原因而變得獨一無二。 –

0

修復方法是刪除models.py中的表規則,然後遷移數據庫,從而強制刪除它。在病房之後,我又加入了規則並再次遷移。這一次它認可了Rules.rule不是一個唯一的密鑰並正確遷移。

感謝您的幫助球員。我不知道爲什麼它沒有發現我在遷移過程中將規則更改爲標準整數,但一切都奏效了。