jeudi 21 mai 2015

SQLite : On delete cascade

I found a lot of questions on stackoverflow and other forums about the "ON DELETE CASCADE issue" with SQLite but in many cases, the answer given is "add this line : 'PRAGMA foreign_keys=ON'". I tried it but it doesn't work. So tried to re-create my tables, it's very simple I just want to do an ON DELETE CASCADE between two tables only so I really don't understand why it doesn't work ... Anyway here are my tables :

CREATE TABLE `realisation` (
`RE_ID` INTEGER NOT NULL,
`AFFAIRE_ID`    INTEGER,
`PARTIE_ID` INTEGER,
`TITRE_ID`  INTEGER,
`QAL_ID`    INTEGER,
PRIMARY KEY(RE_ID),
FOREIGN KEY(`AFFAIRE_ID`) REFERENCES affaire ( AFFAIRE_ID ),
FOREIGN KEY(`PARTIE_ID`) REFERENCES partie ( PARTIE_ID ),
FOREIGN KEY(`TITRE_ID`) REFERENCES titre ( TITRE_ID ),
FOREIGN KEY(`QAL_ID`) REFERENCES qualitification ( QAL_ID ));

CREATE TABLE `qualitification` (
`QAL_ID`    INTEGER NOT NULL,
`QA_ID` INTEGER,
`RE_ID` INTEGER,
PRIMARY KEY(QAL_ID),
FOREIGN KEY(`QA_ID`) REFERENCES qualite ( QA_ID ) ON DELETE CASCADE);

So when I write this query : "DELETE FROM realisation WHERE RE_ID = 1;", I want to delete the line in qualitification where the RE_ID is 1 ...

Actually it's for a .NET app and at the moment, I have to make multiple queries and to delete the RE_ID from both tables, when I have to make this for about 16 tables that's a little bit stupid.

So is there a solution to make it work ? Did I miss something with the foreign key ???

Thanks in advance !

Aucun commentaire:

Enregistrer un commentaire