mysql/multi delete语句-#无法删除或更新父行

wf82jlnq  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(185)

我试图创建过程来删除类别和它们之间的连接,但不幸的是,我不知道如何处理我的关系。


# 1451 - Cannot delete or update a parent row: a foreign key constraint fails (`saver`.`categorytree`, CONSTRAINT `categorytree_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `category` (`id`))

以下是我创建的代码:

delete con, cc from categorytree con 
join categorytree ct on con.child_id = ct.child_id
join category cc on con.child_id = cc.id
where con.parent_id = 1

table:

create table category (
id int not null AUTO_INCREMENT,
name varchar (50) not null,
primary key(id) 
);

create table categorytree(
parent_id int not null,
child_id int not null ,
depth int,
FOREIGN KEY (parent_id) references category(id),
FOREIGN KEY (child_id) references category(id)
);


有人能帮忙吗?
编辑: ON DELETE CASCADE 为我做这件事。谢谢。

create table categorytree(
parent_id int not null,
child_id int not null ,
depth int,
FOREIGN KEY (parent_id) references category(id) ON DELETE CASCADE,
FOREIGN KEY (child_id) references category(id) ON DELETE CASCADE
);

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题