I'm really new to ON DELETE CASCADE. How can I delete child comments when the parent comment gets deleted in a table?
Table Schema
'CREATE TABLE `comments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parent` int(11) NOT NULL,
`comment` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=135 DEFAULT CHARSET=latin1'
Current Query (fails)
ALTER TABLE comments
ADD CONSTRAINT `delete_child`
FOREIGN KEY (`parent`)
REFERENCES `comments` (`id`)
ON DELETE CASCADE
Error Code: 1452.
Cannot add or update a child row: a foreign key constraint fails
(`App`.`#sql-1405_16b7`, CONSTRAINT `delete_child` FOREIGN KEY (`parent`)
REFERENCES `comments` (`id`) ON DELETE CASCADE)
This SQLFiddle shows my situation. It works in SQLFiddle but not in MySQLWorkbench.