SQLite Foreign key ignored

Colin,

I tried both, without avail. Fixed statement does not allow you to include variables, prepare does and is cleaner and safer then via msg.topic. But i will try a 'batch without response as this will allow multiple statements.

Have you got multiple processes writing to the database? If not then then you could do it by checking to see if there are any references to it first and only delete if none. I know that is not ideal, but it would at least get you up and running.
In fact could you do it in a single statement by including in the where clause that COUNT of references to it is 0?

Did you set any foreign key constraints?

Methinks someone didn't read the first post in the thread :slight_smile:

@Colin if you are referring to me Colin, I see no reference to ON UPDATE ... or ON DELETE... in the SQL provided in the first or any post. However after spending two days babysitting two of the grandsons, I could have missed anything (is it Monday yet???):crazy_face:

My understanding (which may be faulty) is that just by providing the foreign key definition that implicitly provides the constraint. The ON UPDATE and ON DELETE modifiers allow for such actions as cascading deletes through the linked records rather than triggering errors. So in the example @AsHex_66 provides, deleting an artist who has linked tracks should generate the error.

So I just did a test and if you add ON DELETE CASCADE to the table and then delete an artist, the titles of that artist DO NOT get deleted.

However, if you issue a `PRAGMA foreign_keys = ONE before deleting the artist, the titles for that artist ARE deleted the way the information I referred to above explains.

Here is the flow , sorry no comments in the flow since I’m away from my computer

[{"id":"bc584f19.2e6128","type":"tab","label":"SQLITE TEST","disabled":false,"info":""},{"id":"95efe235.9ee4","type":"inject","z":"bc584f19.2e6128","name":"CREATE TABLE artist","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":220,"wires":[["a7b54bbb.bcd678"]]},{"id":"28970b24.c21bbc","type":"sqlite","z":"bc584f19.2e6128","mydb":"e839dd59.715d78","sqlquery":"msg.topic","sql":"PRAGMA foreign_keys = ON;\ndelete from artist\nwhere artistid = 3","name":"","x":740,"y":340,"wires":[["d3f2d01a.7d7138"]]},{"id":"17338de8.96634a","type":"comment","z":"bc584f19.2e6128","name":"Data Base example inside-- ","info":"-- DataBase example\n\nCREATE TABLE artist(\n artistid INTEGER PRIMARY KEY, \n artistname TEXT\n);\n\nCREATE TABLE track(\n trackid INTEGER,\n trackname TEXT, \n trackartist INTEGER -- Must map to an artist.artistid!\n FOREIGN KEY(trackartist) REFERENCES artist(artistid)","x":180,"y":160,"wires":[]},{"id":"ed1dc042.ddc0b8","type":"catch","z":"bc584f19.2e6128","name":"","scope":null,"uncaught":false,"x":200,"y":80,"wires":[["1603f487.c5a72b"]]},{"id":"d3f2d01a.7d7138","type":"debug","z":"bc584f19.2e6128","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1030,"y":340,"wires":[]},{"id":"a7b54bbb.bcd678","type":"template","z":"bc584f19.2e6128","name":"","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"CREATE TABLE artist(\n artistid INTEGER PRIMARY KEY, \n artistname TEXT\n);\n","output":"str","x":360,"y":220,"wires":[["28970b24.c21bbc","77521801.50b35"]]},{"id":"77521801.50b35","type":"debug","z":"bc584f19.2e6128","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":710,"y":220,"wires":[]},{"id":"1603f487.c5a72b","type":"debug","z":"bc584f19.2e6128","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":470,"y":80,"wires":[]},{"id":"ad60cc5d.df06b8","type":"inject","z":"bc584f19.2e6128","name":"DROP TABLE artist","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":560,"wires":[["5ade94e2.c253cc"]]},{"id":"5ade94e2.c253cc","type":"template","z":"bc584f19.2e6128","name":"","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"DROP TABLE artist;","output":"str","x":360,"y":560,"wires":[["28970b24.c21bbc"]]},{"id":"2dba2d29.d52372","type":"inject","z":"bc584f19.2e6128","name":"CREATE TABLE track","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":260,"wires":[["76bd86bc.0f347"]]},{"id":"76bd86bc.0f347","type":"template","z":"bc584f19.2e6128","name":"","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"CREATE TABLE track(\n trackid INTEGER,\n trackname TEXT, \n trackartist INTEGER,\n FOREIGN KEY (trackartist) REFERENCES artist (artistid)\n );","output":"str","x":360,"y":260,"wires":[["28970b24.c21bbc"]]},{"id":"f4782433.a02bd8","type":"inject","z":"bc584f19.2e6128","name":"DROP TABLE track","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":600,"wires":[["a942d7f5.bb758"]]},{"id":"a942d7f5.bb758","type":"template","z":"bc584f19.2e6128","name":"","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"DROP TABLE track;","output":"str","x":360,"y":600,"wires":[["28970b24.c21bbc"]]},{"id":"e80993b8.748c38","type":"inject","z":"bc584f19.2e6128","name":"INSERT INTO artist","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":320,"wires":[["e5d555c2.17fd6"]]},{"id":"e5d555c2.17fd6","type":"template","z":"bc584f19.2e6128","name":"","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"INSERT INTO artist(artistid, artistname)\nVALUES\n   (1,'abel'),\n   (2,'paul')\n;\n","output":"str","x":360,"y":320,"wires":[["28970b24.c21bbc"]]},{"id":"69c2ce26.cc6ab8","type":"inject","z":"bc584f19.2e6128","name":"INSERT INTO track","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":360,"wires":[["389e7ac5.d267ee"]]},{"id":"389e7ac5.d267ee","type":"template","z":"bc584f19.2e6128","name":"","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"INSERT INTO track(trackid, trackname, trackartist)\nVALUES\n   (1,'minecraft love songs', 1),\n   (2,'I got the nether blues', 1),\n   (3,'I forgot my sword and now I face a horde', 1),\n   (1, 'The whats for dinner blues',2),\n   (2, 'no more screen time scream',2),\n   (3, 'flying...oops no wings',2)\n;\n","output":"str","x":360,"y":360,"wires":[["28970b24.c21bbc"]]},{"id":"b0295459.6123b8","type":"inject","z":"bc584f19.2e6128","name":"SELECT * FROM artist","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":440,"wires":[["6d5874ef.b628c4"]]},{"id":"6d5874ef.b628c4","type":"template","z":"bc584f19.2e6128","name":"","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"SELECT * FROM artist;\n","output":"str","x":360,"y":440,"wires":[["28970b24.c21bbc"]]},{"id":"ed04c11b.36af28","type":"inject","z":"bc584f19.2e6128","name":"SELECT * FROM track","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":480,"wires":[["804ad2dc.5d5018"]]},{"id":"804ad2dc.5d5018","type":"template","z":"bc584f19.2e6128","name":"","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"SELECT * FROM track;","output":"str","x":360,"y":480,"wires":[["28970b24.c21bbc"]]},{"id":"fcb29800.2f48d","type":"inject","z":"bc584f19.2e6128","name":"PRAGMA foreign_keys;","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":700,"wires":[["76c5e614.9393"]]},{"id":"76c5e614.9393","type":"template","z":"bc584f19.2e6128","name":"","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"PRAGMA foreign_keys;","output":"str","x":360,"y":700,"wires":[["28970b24.c21bbc"]]},{"id":"96d0956.80839e8","type":"inject","z":"bc584f19.2e6128","name":"PRAGMA foreign_keys = ON;","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":760,"wires":[["ddb4e953.a2dd98"]]},{"id":"ddb4e953.a2dd98","type":"template","z":"bc584f19.2e6128","name":"","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"PRAGMA foreign_keys = ON;","output":"str","x":420,"y":760,"wires":[["28970b24.c21bbc"]]},{"id":"31d14a94.6b2c6e","type":"inject","z":"bc584f19.2e6128","name":"DELETE FROM artist","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":740,"y":480,"wires":[["440b3750.f38c88"]]},{"id":"440b3750.f38c88","type":"template","z":"bc584f19.2e6128","name":"","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"DELETE FROM artist\nWHERE artistname = 'paul'\n;","output":"str","x":940,"y":480,"wires":[["28970b24.c21bbc"]]},{"id":"188d352d.1e291b","type":"inject","z":"bc584f19.2e6128","name":"CREATE TABLE artist","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":820,"y":80,"wires":[["11dc1fc1.bdc23"]]},{"id":"11dc1fc1.bdc23","type":"template","z":"bc584f19.2e6128","name":"","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"CREATE TABLE artist(\n artistid INTEGER PRIMARY KEY, \n artistname TEXT\n);\n","output":"str","x":1020,"y":80,"wires":[["28970b24.c21bbc"]]},{"id":"9fb7c2c1.062708","type":"inject","z":"bc584f19.2e6128","name":"CREATE TABLE track","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":820,"y":120,"wires":[["eda2dce2.32767"]]},{"id":"eda2dce2.32767","type":"template","z":"bc584f19.2e6128","name":"","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"CREATE TABLE track(\n trackid INTEGER,\n trackname TEXT, \n trackartist INTEGER,\n FOREIGN KEY (trackartist) \n REFERENCES artist (artistid)\n on delete cascade\n );","output":"str","x":1020,"y":120,"wires":[["28970b24.c21bbc"]]},{"id":"e839dd59.715d78","type":"sqlitedb","z":"","db":"/tmp/test2.db","mode":"RWC"}]

I believe that @AsHex_66 does not want the record to be deleted, he/she wants it to throw an error. That is what I understood anyway.

@colin, You are correct. The record should not be deleted as there is a foreign key constraint.
@zenofmud In the example I gave, the SQL code is in the comment.

I did try your example, and the problem was that I did not send the pragma command individually.
I did combine that in the command. When separated, it works well. Thanks for your help.

(Do you sleep zenofmud ??? :grin: you appear to be online 24-7)

Thanks, appreciated...

Well It seems to me you should use ON DELETE RESTRICT

RESTRICT

The RESTRICT action does not allow you to change or delete values in the parent key of the parent table.