I have the following tables:
CREATE TABLE workspace (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
uid VARCHAR(255),
name NVARCHAR(255)
);
CREATE TABLE userGroup (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
workspaceId INT,
FOREIGN KEY (workspaceId) REFERENCES workspace(id) ON DELETE CASCADE,
name NVARCHAR(255)
);
CREATE TABLE userWorkspaceMapping (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
workspaceId INT,
FOREIGN KEY (workspaceId) REFERENCES workspace(id) ON DELETE CASCADE,
userId INT,
FOREIGN KEY (userId) REFERENCES user(id),
userGroupId INT,
FOREIGN KEY (userGroupId) REFERENCES userGroup(id) ON DELETE CASCADE
);
And this data for userWorkspaceMapping:
+----+-------------+--------+-------------+
| id | workspaceId | userId | userGroupId |
+----+-------------+--------+-------------+
| 1 | 1 | 1 | 1 | <------- I want to delete only this row
| 2 | 2 | 1 | 3 |
| 3 | 3 | 1 | 5 |
+-----------------------------------------+
When I delete a workspace, all three rows in userWorkspaceMapping are deleted, instead of just the first one.
delete from workspace
where id = 1;
Why is everything being deleted in userWorkspaceMapping?
Here's the fiddle: https://www.db-fiddle.com/f/tHMZy2rnyvoHH13E7dS99A/1