I’m trying to create the following sql tables’ structure, but I can’t figure out the problem. Here’s an example:
DROP TABLE IF EXISTS c;
DROP TABLE IF EXISTS b;
DROP TABLE IF EXISTS a;
CREATE TABLE a (
"Field1" INTEGER PRIMARY KEY
);
CREATE TABLE b (
"Field1" INTEGER,
"Field2" INTEGER,
FOREIGN KEY("Field2") REFERENCES "a"("Field1"),
PRIMARY KEY("Field1","Field2")
);
CREATE TABLE c (
"Field1" INTEGER PRIMARY KEY,
"Field2" INTEGER,
"Field3" INTEGER,
FOREIGN KEY("Field2") REFERENCES "b"("Field1"),
FOREIGN KEY("Field3") REFERENCES "a"("Field1") -- or FOREIGN KEY("Field3") REFERENCES "b"("Field2")
);
insert into a values(1);
insert into b values(2,1);
insert into c values(3,2,1);
The last insert results in an error
foreign key mismatch – “c” referencing “b”
What’s the problem?