KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Suppose i have two below table: CREATE TABLE post ( id bigint(20) NOT NULL AUTO_INCREMENT, text text , PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=1; CREATE TABLE post_path ( ancestorid bigint(20) NOT NULL DEFAULT '0', descendantid bigint(20) NOT NULL DEFAULT '0', length int(11) NOT NULL DEFAULT '0', PRIMARY KEY (ancestorid,descendantid), KEY descendantid (descendantid), CONSTRAINT f_post_path_ibfk_1 FOREIGN KEY (ancestorid) REFERENCES post (id) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT f_post_path_ibfk_2 FOREIGN KEY (descendantid) REFERENCES post (id) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB; And inserted these rows: INSERT INTO post (text) VALUES ('a'); #// inserted row by id=1 INSERT INTO post_path (ancestorid ,descendantid ,length) VALUES (1, 1, 0); When i want to update post row id: UPDATE post SET id = '10' WHERE post.id =1 MySQL said: #1452 - Cannot add or update a child row: a foreign key constraint fails (test.post_path, CONSTRAINT f_post_path_ibfk_2 FOREIGN KEY (descendantid) REFERENCES post (id) ON DELETE CASCADE ON UPDATE CASCADE) Why? what is wrong? Edit: When i inserted these rows: INSERT INTO post (text) VALUES ('b'); #// inserted row by id=2 INSERT INTO post_path (ancestorid, descendantid, length) VALUES (1, 2, 0); And updated: UPDATE post SET id = '20' WHERE post.id =2 Mysql updated successfully both child and parent row. so Why i can not update first post (id=1)?
Tags (comma-separated)
Save Edits
Cancel