Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite "On delete CASCADE" not cascading in Qt

Tags:

c++

sqlite

qt

I have a database in Qt. it has four tables: maingroup, subgroup, parts, and position.this is my database:

CREATE TABLE `maingroup` (
    `groupName`TEXT NOT NULL UNIQUE,
     PRIMARY KEY(`groupName`)
);
CREATE TABLE `subgroup` (
    `sub`   TEXT NOT NULL UNIQUE,
    `main`  TEXT NOT NULL,
    PRIMARY KEY(`sub`),
    FOREIGN KEY(`main`) REFERENCES `maingroup`(`groupName`) ON DELETE CASCADE
);
CREATE TABLE `parts` (
    `ID`    INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    `Part_Number`   TEXT,
    `Type`  TEXT NOT NULL,
    `Value` TEXT,
    `Voltage`   TEXT,
    `Quantity`  TEXT,
    `Position`  TEXT,
    `Picture`   TEXT,
    FOREIGN KEY(`Position`) REFERENCES `Position`(`Poistion`) ON DELETE CASCADE,
    FOREIGN KEY(`Type`) REFERENCES `subgroup`(`sub`) ON DELETE CASCADE
);

Type in table parts is foreign key refers to column sub from table subgroup. main in table subgroup is foreign key refers to column groupname in table maingroup. my problem is when I try (delete from maingroup WHERE groupName= 'dd';) in DB Browser it deletes both parent and children. But in QT this command(myQuery.exec("delete from maingroup WHERE groupName= 'dd'");) just deletes the parent field in maingroup table and not the child in subgroup and part table and the main column in subgroup table refers to a field in maingroup table that does not exist. what is wrong here?what should i do?

like image 862
Yousef Avatar asked Nov 25 '18 12:11

Yousef


People also ask

Does SQLite support on delete cascade?

A foreign key with cascade delete means that if a record in the parent table is deleted, then the corresponding records in the child table will automatically be deleted. This is called a cascade delete in SQLite. A foreign key with a cascade delete can only be defined in a CREATE TABLE statement.

Can cascade be used with on delete command?

Use the ON DELETE CASCADE option to specify whether you want rows deleted in a child table when corresponding rows are deleted in the parent table. If you do not specify cascading deletes, the default behavior of the database server prevents you from deleting data in a table if other tables reference it.

Is on delete cascade a constraint?

ON DELETE CASCADE constraint is used in MySQL to delete the rows from the child table automatically, when the rows from the parent table are deleted. For example when a student registers in an online learning platform, then all the details of the student are recorded with their unique number/id.

Is on delete cascade a trigger?

Sadly cascaded deletes do not activate triggers in MySQL.


1 Answers

You need to turn on the foreign-key pragma by executing another statement before your DELETE statement.

QSqlQuery q;
q.exec("PRAGMA foreign_keys = ON");
q.exec("DELETE FROM ...");

This was able to cascade deletes, and should also be sufficient to solve other foreign-key related issues.

Credits to this forum.qt.io post.

like image 67
TrebledJ Avatar answered Sep 27 '22 22:09

TrebledJ