I am trying to move content from wordpress posts to mezzanine using:
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');
but I get
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (
mezdb
.blog_blogpost
, CONSTRAINTsite_id_refs_id_ac21095f
FOREIGN KEY (site_id
) REFERENCESdjango_site
(id
))
mezdb schema:
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)
);
I'm noob in SQL so appreciate your hints.
Try this:
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;
Note: this is an easy workaround, but definitely not a good idea. Or you should be very well aware of what you're doing.
The main point of foreign keys is to maintain the referential integrity, in other words, data consistency. Simply said, the database doesn't allow you to insert wrong value to a column with the foreign key. Now it has to be clear that disabling the foreign key check to be able to insert a wrong value smells very fishy.
See StuartLC's answer for a proper solution.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments