Answered by
lovasoa
Feb 22, 2024
Replies: 1 comment 1 reply
|
Hello ! This is a very clear and well-detailed issue report, thank you. The answer to your question is in this page of the sqlite documentation: https://www.sqlite.org/foreignkeys.html
The third point means that you cannot declare a foreign key constraint to INSTALLATIONID if it is not the primary key of INSTALLATIONS. It is quite counter-intuitive that the error is reported at insertion time instead of at the time when the foreign key is defined, but this is how sqlite works. PRAGMA foreign_keys = ON;
CREATE TABLE IF NOT EXISTS INSTALLATIONS(INSTALLATIONID INTEGER NOT NULL); -- this allows multiple rows in the table to share the same INSTALLATIONID, probably not what you want anyway
CREATE TABLE IF NOT EXISTS TICKETS(INSTALLATION INTEGER NOT NULL, FOREIGN KEY (INSTALLATION) REFERENCES INSTALLATIONS(INSTALLATIONID));
INSERT INTO INSTALLATIONS VALUES (1632);
INSERT INTO TICKETS VALUES (1632);
-- Error: in prepare, foreign key mismatch - "TICKETS" referencing "INSTALLATIONS" (1)but PRAGMA foreign_keys = ON;
CREATE TABLE IF NOT EXISTS INSTALLATIONS(INSTALLATIONID INTEGER PRIMARY KEY);
CREATE TABLE IF NOT EXISTS TICKETS(INSTALLATION INTEGER NOT NULL, FOREIGN KEY (INSTALLATION) REFERENCES INSTALLATIONS(INSTALLATIONID));
INSERT INTO INSTALLATIONS VALUES (1632);
INSERT INTO TICKETS VALUES (1632);
-- no errorTLDR The solution is to change your definition of the INSTALLATIONS table to make INSTALLATIONID the primary key. |
1 reply
Answer selected by
a-gonzalo
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment



Hello !
This is a very clear and well-detailed issue report, thank you.
The answer to your question is in this page of the sqlite documentation: https://www.sqlite.org/foreignkeys.html