【发布时间】:2011-07-12 22:14:20
【问题描述】:
我有一个新表,它对旧的旧表具有外键约束。旧表中填充了大量数据,但是当我尝试向引用旧表中的一行的新表中添加一行时,我收到 Cannot add or update a child row: a foreign key constraint fails 错误。
如何将引用旧表中行的行添加到新表中?
编辑 这是我尝试过的两个查询:
mysql> select user_pk from users where username = 'test_user';
+---------+
| user_pk |
+---------+
| 123766 |
+---------+
1 row in set (0.00 sec)
mysql> insert into uservisit (user_id) values (123766);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test_database`.`uservisit`, CONSTRAINT `user_id_refs_user_pk_37c3999c` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_pk`))
我做错了吗?
【问题讨论】:
-
您确定要插入新表的值存在于旧表中?
-
看看这里的cmets有没有回答你的问题:stackoverflow.com/questions/1253459/…
标签: mysql foreign-keys