I am running the following code from the python interpreter and expect the insert statement to fail and throw some kind of exception. But this does not happen:
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> >>>
Does anyone know why this does not want to work? I understand that the insertion should fail, because the value that I gave for t2(i) is not the primary key in t1 , but, fortunately, is it anyway ...?
python foreign-keys sqlite3
Carson myers
source share