Server: MariaDB 10.3.21
Client: MariaDB 10.4.12
Given the following structure/data:
DROP TABLE IF EXISTS `main`;CREATE TABLE `main` ( `name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL, `description` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL, PRIMARY KEY (`name`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;LOCK TABLES `main` WRITE;INSERT INTO `main` VALUES ('bar','this is another test');INSERT INTO `main` VALUES ('foo','this is a test');UNLOCK TABLES;DROP TABLE IF EXISTS `referenceData`;CREATE TABLE `referenceData` ( `primaryName` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL, `secondaryName` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL, PRIMARY KEY (`primaryName`), KEY `referenceData_FK_1` (`secondaryName`), CONSTRAINT `referenceData_FK` FOREIGN KEY (`primaryName`) REFERENCES `main` (`name`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `referenceData_FK_1` FOREIGN KEY (`secondaryName`) REFERENCES `main` (`name`) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;LOCK TABLES `referenceData` WRITE;INSERT INTO `referenceData` VALUES ('bar','bar');INSERT INTO `referenceData` VALUES ('foo','bar');UNLOCK TABLES;
One can update/delete row 1 in table main
just fine and the CASCADE performs as expected on table referenceData
. However, when one attempts to update row 2 in table main
(e.g. via UPDATE main SET name = 'bar2' WHERE name = 'bar'
), the following error is returned:
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`testdb`.`referenceData`, CONSTRAINT `referenceData_FK_1` FOREIGN KEY (`secondaryName`) REFERENCES `main` (`name`) ON DELETE CASCADE ON UPDATE CASCADE)
If one attempts a delete via DELETE FROM main WHERE name = 'bar'
, however, it works fine.
What, quite frankly, in tarnation? Why does this error occur when both columns are the same value during an update, and why isn't a delete affected?