我有两个刚发现的数据库表,在特定列上没有外键关系。这些列均为VARCHAR(45) NON NULL
,但出于任何原因它们都抛出ERROR 1452: Cannot add or update a child row: a foreign key constraint fails
。我按照许多关于SO的建议进行了SHOW ENGINE INNODB STATUS;
的操作,并给出了以下错误提示框
LATEST FOREIGN KEY ERROR
------------------------
2019-04-02 17:08:10 0x3a50 Transaction:
TRANSACTION 1679595, ACTIVE 0 sec inserting, thread declared inside InnoDB 4965
mysql tables in use 2, locked 2
9 lock struct(s), heap size 1136, 22 row lock(s), undo log entries 18
MySQL thread id 53, OS thread handle 14928, query id 341954 localhost ::1 root copy to tmp table
ALTER TABLE `dbtest`.`task`
ADD CONSTRAINT `FK_planning_recurring_uid`
FOREIGN KEY (`recurring_uid`)
REFERENCES `dbtest`.`planning` (`recurring_uid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
Foreign key constraint fails for table `dbtest`.`#sql-f70_35`:
,
CONSTRAINT `FK_planning_recurring_uid` FOREIGN KEY (`recurring_uid`) REFERENCES `planning` (`recurring_uid`) ON DELETE NO ACTION ON UPDATE NO ACTION
Trying to add in child table, in index FK_planning_recurring_uid_idx tuple:
DATA TUPLE: 2 fields;
0: len 36; hex 61393062326434362d313737632d343237302d396537662d396138363766346235363635; asc a90b2d46-177c-4270-9e7f-9a867f4b5665;;
1: len 4; hex 80000028; asc (;;
But in parent table `dbtest`.`planning`, in index recurring_uid_index,
the closest match we can find is record:
PHYSICAL RECORD: n_fields 2; compact format; info bits 0
0: len 30; hex 61393233626432322d623439332d343662392d623435312d393964363363; asc a923bd22-b493-46b9-b451-99d63c; (total 36 bytes);
1: len 4; hex 80050ffd; asc ;;
这不是我所希望的明确错误。如果不先清空数据库就不能创建具有空值的外键,还是我做错了什么?我已经检查了一个表中存在的值,而另一表中不存在,但找不到任何值。
答案 0 :(得分:0)
通过在插入外键之前先进行SET FOREIGN_KEY_CHECKS=0
,然后再进行SET FOREIGN_KEY_CHECKS=1
来解决此问题。