2015-02-24 84 views
2

我創建了金字塔SQLAlchemy的和我的models.py類包含以下內容:刪除約束在SQLAlchemy的

class Students(Base): 

    __tablename__ = 'students' 
    id = Column(Integer, primary_key=True, nullable=False, autoincrement=True) 
    name = Column(String(50, convert_unicode=True), nullable=False, unique=True) 

我無意中創造了獨特的約束奔着initializedb.py命令和模式被創造。我嘗試刪除唯一約束並重新運行initializedb.py命令,但約束不會丟失,並且出現此錯誤。

sqlalchemy.exc.IntegrityError: (IntegrityError) column name is not unique u'INSERT INTO students (id,name) VALUES (?, ?)' (200,'Hailey') 

如何刪除/修改此約束?

回答

4

金字塔initialize_*_db腳本調用MetaData.create_all,這是一個非常愚蠢的函數,因爲它對任何具有相同名稱的表(如果存在)感到滿意。不幸的是,你需要手動刪除唯一約束,或者刪除整個數據庫並重新創建。

對於任何嚴重的項目,我建議您使用Alembic來處理數據庫模式遷移。

+0

呵呵:(如果我刪除自動創建的.sqlite數據庫並重新運行初始化的b命令,是否就足夠了? – Tania 2015-02-24 06:46:06

+1

是的,這將是「丟棄數據庫」在sqlite3 – 2015-02-24 06:49:08

+0

像魅力一樣工作.. – Tania 2015-02-24 09:25:40