MySQL错误1452(23000):无法添加或更新子行:外键约束失败
我正在尝试使用以下方式将内容从wordpress帖子移动到夹层:MySQL错误1452(23000):无法添加或更新子行:外键约束失败,mysql,sql,wordpress,data-migration,mezzanine,Mysql,Sql,Wordpress,Data Migration,Mezzanine,我正在尝试使用以下方式将内容从wordpress帖子移动到夹层: INSERT INTO mezdb.blog_blogpost (id, user_id, publish_date, content, title, description, slug, updated, status) SELECT DISTINCT ID, post_author, post_date, post_content, post_title, post_excerpt, post_n
INSERT INTO mezdb.blog_blogpost (id, user_id, publish_date,
content, title, description, slug, updated, status)
SELECT DISTINCT ID, post_author, post_date, post_content, post_title, post_excerpt,
post_name, post_modified, post_status
FROM wpdb.wp_posts WHERE wp_posts.post_type IN ('story');
但我明白了
错误1452 23000:无法添加或更新子行:外键
约束失败mezdb.blog_blogpost,约束
站点id参考站点id ac21095f外键站点id参考
django_站点id
mezdb模式:
CREATE TABLE `blog_blogpost` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`comments_count` integer NOT NULL,
`keywords_string` varchar(500) NOT NULL,
`rating_count` integer NOT NULL,
`rating_sum` integer NOT NULL,
`rating_average` double precision NOT NULL,
`site_id` integer NOT NULL,
`title` varchar(500) NOT NULL,
`slug` varchar(2000),
`_meta_title` varchar(500),
`description` longtext NOT NULL,
`gen_description` bool NOT NULL,
`created` datetime,
`updated` datetime,
`status` integer NOT NULL,
`publish_date` datetime,
`expiry_date` datetime,
`short_url` varchar(200),
`in_sitemap` bool NOT NULL,
`content` longtext NOT NULL,
`user_id` integer NOT NULL,
`allow_comments` bool NOT NULL,
`featured_image` varchar(255)
);
我不懂SQL,所以非常感谢您的提示。django_site表应该至少包含一条记录,blog_blogpost中的每条记录都必须有一个对它的引用-blog_blogpost.site_id字段是对django_site.id的引用,如错误消息所示
因此,您只需在查询中输入一个常量:
INSERT INTO mezdb.blog_blogpost(
id,
user_id,
publish_date,
content,
title,
description,
slug,
updated,
status,
site_id) -- This is a reference field
SELECT
DISTINCT id,
post_author,
post_date,
post_content,
post_title,
post_excerpt,
post_name,
post_modified,
post_status,
1 -- This is your constant, it may be different - look it up in 'django_site'
FROM wpdb.wp_posts WHERE wp_posts.post_type IN ('story');
试试这个:
set foreign_key_checks=0;
INSERT INTO mezdb.blog_blogpost (id, user_id, publish_date,
content, title, description, slug, updated, status)
SELECT DISTINCT ID, post_author, post_date, post_content, post_title, post_excerpt,
post_name, post_modified, post_status
FROM wpdb.wp_posts WHERE wp_posts.post_type IN ('story');
set foreign_key_checks=1;
注意:这是一个简单的解决方法,但绝对不是一个好主意。或者你应该非常清楚自己在做什么
换句话说,数据的主要目的是保持数据的一致性。简单地说,数据库不允许使用外键向列插入错误的值。现在必须清楚的是,禁用外键检查以能够插入错误的值闻起来非常可疑
请参阅StuartLC的答案以获得正确的解决方案。站点id定义为整数非空,并且根据错误消息,具有django_站点id的外键
您尚未在插入列表中指定站点id。您需要为site_id包含一个值,并给它一个存在于表django_site的列id中的值
或者,如果无法确定有效的site_id值,请将blog_blogpost表列site_id更改为允许NULL,即site_id整数NULL。显然,博客Post和Django_站点之间的内部连接现在需要重新考虑
错误1452 23000:无法添加或更新子行:外键
约束失败mezdb.blog_blogpost,约束
站点id参考站点id ac21095f外键站点id参考django站点
身份证
表blog_blogpost架构:
站点id整数不为空
这意味着site_id不是NULL字段,从错误消息中我们可以得出结论,它引用了另一个表的django_siteid
问题的根本原因:
在插入查询中,您没有传递siteid
请记住,您传递的site_id值应该在django_siteid列中,因为它被引用为外键
解决方案:
传递引用表的django_siteid列中存在的Site_id值。
我想有些行已经插入了,那么您需要更新它。不插入。这个错误给出:错误1452 23000:无法添加或更新子行:外键约束失败mezdb.blog\u blogpost,约束用户_-id_-refs_-id_01a962b8外键用户_-id引用身份验证用户id。我尝试了除1以外的其他数字。没有effect@qliq这是一个不同的错误,但问题是相同的。您的auth_user表可能为空,因此另一个约束失败。您应该首先迁移用户,保留他们的ID,然后就可以了,除非其他约束失败。其思想是:首先迁移没有约束的表,然后迁移引用这些表的表,当迁移引用那些二阶表的表时,诸如此类——想象一下,您从leaf开始重新创建一棵树,但请稍候。如果禁用了外键检查,那么使用外键有什么意义?如果有人使用你的查询,他只是在他的数据库中创建了一个不一致性。外键的要点是保持数据的一致性,换句话说。在表中创建外键以防止插入错误的值,然后禁用它以插入错误的值是没有意义的。