TRUNCATE does not respect cascading rules
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
Percona-XtraDB |
New
|
Undecided
|
Unassigned |
Bug Description
I've got a couple tables set up with foreign keys to a `User` table. When a `User` record is deleted, I want all the related records deleted. This works perfectly if I delete single or multiple records (i.e. the CASCADE rule is respected), but if I run `TRUNCATE`, the CASCADE rules are suddenly no longer followed.
TRUNCATE TABLE `User`; # Cannot truncate a table referenced in a foreign key constraint (`deploi-
Here are my tables:
CREATE TABLE `User` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`firstName` varchar(255) DEFAULT NULL,
`lastName` varchar(255) DEFAULT NULL,
`email` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email_UNIQUE` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `DeviceComment` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`deviceId` int(11) NOT NULL COMMENT ' ',
`commentId` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_DeviceComme
KEY `fk_DeviceComme
CONSTRAINT `fk_DeviceComme
CONSTRAINT `fk_DeviceComme
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
CREATE TABLE `Device` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`type` enum('PC'
`cost` double(11,2) DEFAULT NULL,
`purchasedOn` timestamp NULL DEFAULT NULL,
`userId` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_Device_User` (`userId`),
CONSTRAINT `fk_Device_User` FOREIGN KEY (`userId`) REFERENCES `User` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
CREATE TABLE `Comment` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`message` text NOT NULL,
`userId` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_Comment_User1` (`userId`),
CONSTRAINT `fk_Comment_User1` FOREIGN KEY (`userId`) REFERENCES `User` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;
affects: | percona-server → percona-xtradb |