我在Amazon RDS上的mysql DB8.0.20版本中有两个表,它们之间有一个外键。
当我尝试插入第二个表时,我得到一个外键错误,但我确定第一个表中存在该键。
SELECT id_column FROM db.table1 WHERE id_column='123';
+------------+
| id_column |
+------------+
| 123 |
+------------+
1 row in set (0.00 sec)
然后我跑:
INSERT INTO db.table2 (foreign_id, ...) VALUES ('123', ...);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`db`.`table2`, CONSTRAINT `table2_ibfk_1` FOREIGN KEY (`foreign_id`) REFERENCES `table1` (`id_column`))
那么,如果ID存在于表1中,为什么表2不能在外键列中使用它呢?我错过了什么?
请确保所有这些操作的数据类型匹配: