我有这张桌子
CREATE TABLE `product_category` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
`category_id` int(11) NOT NULL default '0',
PRIMARY KEY (`id`),
KEY `category_id` (`category_id`),
CONSTRAINT `category_id` FOREIGN KEY (`category_id`) REFERENCES `product_category` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB
我想要得到的是在cascade上删除每个记录哪个category_id字段等于id字段,但是我无法插入任何记录,它会给我一个错误
cannot add or update a child row: a foreign key constraint fails('database/product_category', CONSTRAINT 'category_id FOREIGN KEY('category_id') REFERENCES product_category('id') ON DELETE CASCADE ON UPDATE CASCADE
答案 0 :(得分:2)
您的外键值必须指向现有的product_category (id)
,但表中没有允许的行。所以制作
category_id int(11) NULL,
代替您的定义并插入第一行,如
INSERT INTO product_category SET name='test1';