1. 程式人生 > >MySql資料庫插入或更新報錯:Cannot add or update a child row: a foreign key constraint fails

MySql資料庫插入或更新報錯:Cannot add or update a child row: a foreign key constraint fails

具體報錯資訊:

Cannot add or update a child row: a foreign key constraint fails (`xxx`.`AAA`, CONSTRAINT `tb_competitionmsg_ibfk_2` FOREIGN KEY (`organizerid`) REFERENCES `BBB` (`id`))

 

報錯資訊分析:

XXX資料庫中的 AAA表 中的 organizerid欄位 是BBB表中的id的外來鍵,但是要插入的organizerid值在id中沒有匹配成功。

 

解決:

檢查插入的資料是否正確

在BBB表中的id欄位中新增上所要插入的值。