MySQL - On delete cascade constraint doesn't do it's job properly

Viewed 495

I have a couple of tables:

  • users is full of users.
  • item is full of items.
  • item can have multiple second_items.
  • users are able to have second_items via table users_items.

When I delete a item all second_item and users_items should be removed too. So I setup some constraints that should delete child table records. But it leaves the record in users_items.

Here is my MySQL queries:

CREATE TABLE `item` (
  `id` int(10) UNSIGNED NOT NULL,
  `name` varchar(63) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `item` (`id`, `name`) VALUES (1, 'test');

CREATE TABLE `second_item` (
  `id` int(10) UNSIGNED NOT NULL,
  `item_id` int(10) UNSIGNED DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `second_item` (`id`, `item_id`, `name`) VALUES
(1, 1, 'test');

CREATE TABLE `users` (
  `id` int(10) UNSIGNED NOT NULL,
  `email` varchar(200) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `users` (`id`, `email`, `password`) VALUES (1, 'test', 'test');

CREATE TABLE `users_items` (
  `id` int(10) UNSIGNED NOT NULL,
  `user_id` int(10) UNSIGNED DEFAULT NULL,
  `item_id` int(10) UNSIGNED DEFAULT NULL,
  `item_second_id` int(10) UNSIGNED DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `users_items` (`id`, `user_id`, `item_id`, `item_second_id`) VALUES (1, 1, 1, 1);

ALTER TABLE `second_item`
  ADD PRIMARY KEY (`id`),
  ADD KEY `item_id` (`item_id`);

ALTER TABLE `item` ADD PRIMARY KEY (`id`);

ALTER TABLE `users` ADD PRIMARY KEY (`id`);

ALTER TABLE `users_items`
  ADD PRIMARY KEY (`id`),
  ADD KEY `user_id` (`user_id`),
  ADD KEY `item_id` (`item_id`),
  ADD KEY `item_second_id` (`item_second_id`);


ALTER TABLE `second_item`
  ADD CONSTRAINT `constraint1` FOREIGN KEY (`item_id`) REFERENCES `item` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE `users_items`
  ADD CONSTRAINT `constraint2` FOREIGN KEY (`item_id`) REFERENCES `item` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `constraint3` FOREIGN KEY (`item_second_id`) REFERENCES `second_item` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
  ADD CONSTRAINT `constraint4` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

I try to do the following delete query, but it leaves a record in users_items:

DELETE FROM `item` WHERE `item`.`id` = 1;

This "bug" happens on:

  • WAMP (Server version: 5.7.16 - MySQL Community Server (GPL))
  • UBUNTU (5.7.20-0ubuntu0.16.04.1 - (Ubuntu))

EDITED:

The bug is caused by the order you add constraints:

ALTER TABLE `users_items`
  ADD CONSTRAINT `constraint1` FOREIGN KEY (`item_id`) REFERENCES `item` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `constraint2` FOREIGN KEY (`item_second_id`) REFERENCES `second_item` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
  ADD CONSTRAINT `constraint3` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE `second_item`
  ADD CONSTRAINT `constraint4` FOREIGN KEY (`item_id`) REFERENCES `item` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

This way it is fixed.

1 Answers

I had indeed the same results as you had. Also found the fix ;)

Change the order of your constraints in your SQL code. This will solve your problem.

I think this is a weird MySQL Bug

Related