2013-06-20 167 views
2

我有一個配置爲使用SQLite的Django網絡服務器。SQLite是否支持參照完整性?

在多對多的關係中(有額外的字段),Django迫使我使用關係模型來建立兩個對象之間的關係。但是我能夠在對象之間創建一個關係,這些對象在相關表中還不存在。

對於e.g:

I have table1 and table2 which are related via table12. 
In table1, there is just one object called A. 
In table2, there is just one object called X. 
I can create a record in table12 that depict a relationship between A & Y; even though Y doesn't exist in table2. 

我的關係模式已適當標記的外鍵。

回答

8

SQLite在默認情況下不強制執行foreign key constraints(主要是向後兼容)。

要啓用它,你必須在你之後執行

PRAGMA foreign_keys = 1 

連接到數據庫。

有關更多詳細信息,請參閱有關PRAGMA foreign_keys的手冊。

+1

還發現了一種在Django中啓用它的方法! http://stackoverflow.com/questions/6745763/enable-integrity-checking-with-sqlite-in-django – mynk