Question

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, CONSTRAINT site_id_refs_id_ac21095f FOREIGN KEY (site_id) REFERENCES django_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.

Was it helpful?

Solution

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.

OTHER TIPS

site_id is defined as integer NOT NULL and as per the error message, has a foreign key to django_site (id).

You haven't specified site_id in your insert list. You will need to include a value for site_id and give it a value which exists in table django_site, column id.

Alternatively, if you are not able to determine a valid site_id value, change the blog_blogpost table column site_id to allow NULL (i.e. site_id integer NULL). Obviously INNER JOINS betweenblog_blogpostanddjango_site` will now need to be reconsidered.

django_site table should contain at least one record, and each record in blog_blogpost must have a reference to it - blog_blogpost.site_id field is a reference to django_site.id, as error message says.

So, you just have to put a constant in your query:

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');

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (mezdb.blog_blogpost, CONSTRAINT site_id_refs_id_ac21095f FOREIGN KEY (site_id) REFERENCES django_site (id))

Table blog_blogpost Schema:

The site_id integer NOT NULL

which means site_id is not NULL field and from the error message we can come to the conclusion that it is referenced to the django_site(id)) of the another table.

Root Cause of the problem:

  1. in your insert query you didn't pass siteid
  2. keep in mind that the site_id value your passing should be there in django_site(id)) column as it is referenced as the foreign key

Solution:

  1. Pass Site_id value which is present in django_site(id)) column of the table referenced.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top