MySQL ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails

qliq

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.

Krishna

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails - Laravel

From Dev

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

From Dev

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

From Dev

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

From Dev

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

From Dev

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails - Laravel

From Dev

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

From Dev

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails - laravel

From Dev

Laravel - SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

From Dev

Laravel: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

From Dev

Mysql error 1452 - Cannot add or update a child row: a foreign key constraint fails?

From Dev

MySQL Workbench: Error Code 1452. Cannot add or update a child row: a foreign key constraint fails

From Dev

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails - MYSQL

From Dev

1452 - Cannot add or update a child row: a foreign key constraint fails

From Dev

Foreign Key Exists and I get MySQL Error: 1452: Cannot add or update a child row: a foreign key constraint fails

From Dev

ERROR 1452 : Cannot add or update a child row: a foreign key constraint fails

From Dev

Django Integrity Error 1452, Cannot add or update a child row: a foreign key constraint fails

From Dev

MySQL Workbench. ERROR 1452: Cannot add or update a child row: a foreign key constraint fails. Operation failed

From Dev

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row:a foreign key

From Dev

Laravel 5: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

From Dev

Laravel - Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

From Dev

Laravel: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

From Dev

Laravel Seed: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

From Dev

Cannot add or update a child row: a foreign key constraint fails in MySQL

From Dev

MySQL Error 1452 (23000): Cannot add or update a child row

From Dev

Cannot add or update a child row: a foreign key constraint fails Error in PHP or MySQL?

From Dev

mysql: ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails

From Dev

PDO - Fatal Error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row:

From Dev

unable to update a child table when a foreign key constraint is added: error :Cannot add or update a child row: a foreign key constraint fails

Related Related

  1. 1

    SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails - Laravel

  2. 2

    SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

  3. 3

    SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

  4. 4

    SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

  5. 5

    SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

  6. 6

    SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails - Laravel

  7. 7

    SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

  8. 8

    SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails - laravel

  9. 9

    Laravel - SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

  10. 10

    Laravel: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

  11. 11

    Mysql error 1452 - Cannot add or update a child row: a foreign key constraint fails?

  12. 12

    MySQL Workbench: Error Code 1452. Cannot add or update a child row: a foreign key constraint fails

  13. 13

    Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails - MYSQL

  14. 14

    1452 - Cannot add or update a child row: a foreign key constraint fails

  15. 15

    Foreign Key Exists and I get MySQL Error: 1452: Cannot add or update a child row: a foreign key constraint fails

  16. 16

    ERROR 1452 : Cannot add or update a child row: a foreign key constraint fails

  17. 17

    Django Integrity Error 1452, Cannot add or update a child row: a foreign key constraint fails

  18. 18

    MySQL Workbench. ERROR 1452: Cannot add or update a child row: a foreign key constraint fails. Operation failed

  19. 19

    SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row:a foreign key

  20. 20

    Laravel 5: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

  21. 21

    Laravel - Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

  22. 22

    Laravel: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

  23. 23

    Laravel Seed: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

  24. 24

    Cannot add or update a child row: a foreign key constraint fails in MySQL

  25. 25

    MySQL Error 1452 (23000): Cannot add or update a child row

  26. 26

    Cannot add or update a child row: a foreign key constraint fails Error in PHP or MySQL?

  27. 27

    mysql: ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails

  28. 28

    PDO - Fatal Error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row:

  29. 29

    unable to update a child table when a foreign key constraint is added: error :Cannot add or update a child row: a foreign key constraint fails

HotTag

Archive