2014-01-25 70 views
-1

我想從WordPress的帖子移動內容使用於夾層:MySQL錯誤1452(23000):不能添加或更新子行,外鍵約束失敗

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

,但我得到

ERROR 1452(23000):不能添加或更新子行:外鍵約束 失敗(mezdbblog_blogpost,約束 site_id_refs_id_ac21095f外鍵(site_id)參考文獻 django_siteid))

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小白所以感謝您的提示。

+0

我想有些行已經插入,那麼你需要更新它不插入 –

回答

4

試試這個:

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; 

注:這是一個簡單的解決方案,但絕對不是一個好想法。或者你應該非常清楚你在做什麼。

foreign keys主要的一點是保持the referential integrity,換句話說,數據一致性。簡單地說,數據庫不允許你使用外鍵向列中插入錯誤的值。現在必須明確的是,禁用外鍵檢查能夠插入錯誤的值,非常可疑。

有關正確的解決方案,請參閱StuartLC的答案。

+0

像一個魅力工作。乾杯! – qliq

+0

但是等一下。如果您禁用其檢查,那麼擁有外鍵有什麼意義?如果有人會使用你的查詢,他只是在他的數據庫中創建了不一致。外鍵的要點是保持[參照完整性](https://en.wikipedia.org/wiki/Reference_integrity),換句話說,就是數據的一致性。在表中創建一個外鍵以防止插入錯誤的值,然後禁用它來插入錯誤的值是沒有意義的。 –

2

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

嗯,這個給出了:錯誤1452(23000):無法添加或更新子行:外鍵約束失敗('mezdb '.'blog_blogpost',CONSTRAINT'user_id_refs_id_01a962b8' FOREIGN KEY('user_id')REFERENCES'auth_user'('id'))。我嘗試了其他數字1.不是影響 – qliq

+1

@qliq這是一個不同的錯誤,但問題是一樣的。你的'auth_user'表可能是空的,所以另一個約束失敗。您應該首先遷移用戶,保留他們的ID,然後它應該沒問題,除非其他約束會失敗。這個想法是:**首先遷移沒有約束的表,然後遷移引用這些表的表,當遷移引用這些「二級」表等的表時 - 等等 - 想象你從「葉子」開始重新創建一棵樹。 – scriptin

4

site_id定義爲integer NOT NULL並根據錯誤消息,有一個外鍵爲django_site (id)

您還沒有在插入列表中指定site_id。您需要包含site_id的值,並給它一個存在於表django_site,id列中的值。

或者,如果不能夠確定一個有效site_id值,改變blog_blogpost表列site_id允許NULL(即site_id integer NULL)。顯然INNER JOINS between blog_blogpost and django_site`現在需要重新考慮。

0

錯誤1452(23000):無法添加或更新子行:外鍵 約束失敗(mezdb。blog_blogpost,約束 site_id_refs_id_ac21095f外鍵(SITE_ID)參考django_site (ID))

表blog_blogpost架構:

The site_id整數NOT NULL

這意味着site_id不NULL字段,並從錯誤消息中我們可以得出它被引用到的結論另一張桌子的。問題的

根本原因:

    在你的插入查詢
  1. 你沒有通過siteid
  2. 記住的是,site_id值通過考試應該有在django_site(id))列,因爲它被引用作爲外鍵

解決方案:

  1. 通票Site_id存在於所引用表的django_site(id))列中的值。
相關問題