Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why aren't my sqlite3 foreign keys working?

I run the following code from a python interpreter, and expect the insert statement to fail and throw some kind of exception. But it's not happening:

Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> conn = sqlite3.connect("test.db")
>>> conn.executescript("""
... pragma foreign_keys=on;
... begin transaction;
... create table t1 (i integer primary key, a);
... create table t2 (i, a, foreign key (i) references t1(i));
... commit;
... """)
<sqlite3.Cursor object at 0x0229DAA0>
>>> c = conn.cursor()
>>> c.execute("insert into t2 values (6, 8)")
<sqlite3.Cursor object at 0x0229DAD0>
>>> #???
...
>>> conn.commit()
>>> #???????????
...
>>> c.execute("select * from t2")
<sqlite3.Cursor object at 0x0229DAD0>
>>> c.fetchall()
[(6, 8)]
>>> #but why!?
...
>>>

Does anyone know why this doesn't want to work? My understanding is that the insert should fail since the value I gave for t2(i) isn't a primary key in t1, but it happily does it anyway...?

like image 395
Carson Myers Avatar asked Jul 21 '10 03:07

Carson Myers


People also ask

Does sqlite3 support foreign keys?

SQLite has supported foreign key constraint since version 3.6. 19. The SQLite library must also be compiled with neither SQLITE_OMIT_FOREIGN_KEY nor SQLITE_OMIT_TRIGGER. To check whether your current version of SQLite supports foreign key constraints or not, you use the following command.

How do foreign keys work in SQLite?

A foreign key is a way to enforce referential integrity within your SQLite database. A foreign key means that values in one table must also appear in another table. The referenced table is called the parent table while the table with the foreign key is called the child table.

How do I fix foreign key constraint failure?

The error message itself showing there is a foreign key constraint error, which means you are deleting a parent table where the child table contains the Primary table identifier as a foreign key. To avoid this error, you need to delete child table records first and after that the parent table record.


2 Answers

Working foreign key support in SQLite is very new -- it was only released in 3.6.19 on October 14th. Are you sure you're using SQLite 3.6.19 or later?

Check the sqlite_version constant in the sqlite3 module. E.g. on a Mac OS X 10.6 system with the default python/sqlite install:

>>> import sqlite3
>>> sqlite3.sqlite_version
'3.6.12'
>>> 
like image 198
Nicholas Knight Avatar answered Nov 15 '22 06:11

Nicholas Knight


As said by Nicholas, check if your version of sqlite has foreign key support. This doesn't matter if the version of sqlite greater than or equal to 3.6.19. The source may be compiled with foreign key support turned off. To check execute the following command.

cursor.execute("PRAGMA foreign_keys")

If it does not return any data then your version has no foreign key support.

NB: Foreign key support is not enforced in sqlite3 as of now. Check out here.

like image 28
Charitoo Avatar answered Nov 15 '22 08:11

Charitoo