我创建了 3 个表
用户 { 用户 ID --pk 用户名 }
topics
{
topic_id-- pk
content
}
created_topics
{
created_id--pk
user_id
topic_id
FOREIGN KEY (user_id) REFERENCES users(user_id)
FOREIGN KEY (topic_id) REFERENCES topics(topic_id)
}
当我运行此查询时,
insert into created_topics (user_id,topic_id) values (1,1);
我收到此错误...
#1452 - Cannot add or update a child row: a foreign key constraint fails (`xxxxxxxxxxx`.`created_topics`, CONSTRAINT `created_topics_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`))
有什么想法吗?
最佳答案
任何外键的值必须是主键的值之一。您无法像上面那样将值插入具有外键约束的字段中。首先,将值插入用户和主题表中。然后,您只能使用(引用)在各自的外键中插入到主键中的值。
关于mysql - 插入到不同表时更新外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37036218/