On 04/02/2012 07:22 AM, Pete wrote:
I'm running into an issue with foreign keys where no matter what value I supply for a child key, I get a foreign key mismatch error. Here are my test tables.
"foreign key mismatch" indicates a schema problem. Usually a missing index. See here: http://www.sqlite.org/foreignkeys.html#fk_indexes It's likely you need to create a UNIQUE index on t1.RefColumn.
CREATE TABLE "t1" ("RefColumn" TEXT ,"Data" TEXT ) CREATE TABLE "t2" ("FKeyColumn" TEXT REFERENCES "t1"("RefColumn"),"Data" TEXT ) PRAGMA foreign_keys is set to 1. Put a couple of entries into t1: SELECT * FROM t1 RefColumn Data ---------- ---------- a aaa b bbb Now insert a row into t2 INSERT INTO t2 VALUES ('a','aaa'); I get a foreign Key mismatch error. No matter what value I supply for FKeyColumn, even NULL, I get the same error. I disabled foreign keys, then the INSERT worked. Enabled foreign keys again, it fails again. And even more concerning "DELETE FROM t2" also produces a foreign key mismatch error. What am I doing wrong?
_______________________________________________ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users