TRUNCATE does not respect cascading rules

Bug #1055200 reported by Danny Kopping
6
This bug affects 1 person
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-test`.`Comment`, CONSTRAINT `fk_Comment_User1` FOREIGN KEY (`userId`) REFERENCES `deploi-test`.`User` (`id`))

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_DeviceComment_Device1` (`deviceId`),
  KEY `fk_DeviceComment_Comment1` (`commentId`),
  CONSTRAINT `fk_DeviceComment_Device1` FOREIGN KEY (`deviceId`) REFERENCES `Device` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `fk_DeviceComment_Comment1` FOREIGN KEY (`commentId`) REFERENCES `Comment` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) 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','Phone','Tablet') DEFAULT NULL,
  `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;

Tags: truncate
affects: percona-server → percona-xtradb
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.