

If your database is mostly write only, the additional overhead of. The fact that the FK pragma is NOT PERSISTENT in the DB file is very unfortunate, but exactly what you want in your case. Unless the app you use explicitly turns them ON, on the connection. Just declare your FKs, and they will NOT be enforced. Inserted values into the tables with these 2 commands:Ĭ.execute('INSERT INTO transformers(name) VALUES(4523)')Ĭ.execute('INSERT INTO windings(transformer_id) VALUES(4)') There is, in my mind, only one valid reason to not use foreign keys in a database schema. There’s nothing to do, since FKs are OFF by default. With only one record inserted in the transformers table (which will have id = 1), I shouldn't be able to insert a record into the windings table with transformer_id = 4Ĭreated the following tables with these 2 commands:Ĭ.execute('CREATE TABLE transformers(id INTEGER PRIMARY KEY, weight INTEGER)')Ĭ.execute('CREATE TABLE windings(id INTEGER PRIMARY KEY, transformer_id INTEGER NOT NULL, \įOREIGN KEY(transformer_id) REFERENCES transformers(id))')
#Sqlite foreign key not enforced how to#
Don't know how to fix this? Here is a simplified version of my commands. So the foreign key is not being enforced. Created foreign key constraint but, when I inserted what should be a 'bad' record to my child table, Python did not return an error. By the time foreign key constraint enforcement was added, there were already countless millions of databases in circulation that contained foreign key constraints, some of.

Creating one-to-many relationship between 2 tables. SQLite has parsed foreign key constraints for time out of mind, but added the ability to actually enforce those constraints much later, with version 3.6.19 (). Using sqlite3 (that is loaded with Python) for small work database.
