I try to delete all the subcategories and entries within this subentries, when i delete the main categorie from within one table. If i had multiple tables, i would use a foreign key with ON DELETE CASCADE. But this seems not to work, when i use only a single table. I have the following example table:
CREATE TABLE "data" ( `ID` TEXT NOT NULL, `ParentID` TEXT NOT NULL, `Title` TEXT NOT NULL ) ID is not an INT, because its generated as random UUID. If in ParentID the same value is stored, as in ID, this entry is a subentry to the corresponding one with the same ID. This creates a hierarchy like structure. If i delete now one entry, it should delete also all subentries to the deleted ID (and its sub-sub-sub entries, if there are some).
That's why i tried to use a foreign Key:
CREATE TABLE "data" ( `ID` TEXT, `ParentID` TEXT, `Title` TEXT, FOREIGN KEY (`ParentID`) REFERENCES "data"(`ID`) ON DELETE CASCADE ) But i get an error: foreign key mismatch - "data" referencing "data" (DELETE FROM data WHERE ID = 8;)
BTW: Is there a difference in using the above foreign key definition vs:
CREATE TABLE "data" ( `ID` TEXT, `ParentID` TEXT, `Title` TEXT, CONSTRAINT data_fk1 FOREIGN KEY (`ParentID`) REFERENCES "data"(`ID`) ON DELETE CASCADE )