2017-11-13 7 views
0

リモートWPデータベースからlaravelを使用してローカルDBに一部のコンテンツをインポートしています。私はテーブルの目録と内容を設定しました。Laravel - 整合性制約違反:1452子行を追加または更新できません

棚卸表は次のようになります。私は、リモートWPのDBからの全てと輸入単一の記事をインポートする機能を作った

Schema::create('contents', function (Blueprint $table) { 
     $table->increments('id')->unsigned(); 
     $table->integer('ct_id')->unsigned(); 
     $table->foreign('ct_id')->references('id')->on('content_types'); 
     $table->integer('cms_id'); 
     $table->string('title'); 
     $table->string('slug'); 
     $table->text('excerpt'); 
     $table->mediumText('body'); 
     $table->string('password'); 
     $table->integer('parent_id'); 
     $table->timestamps(); 
    }); 

Schema::create('inventories', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('remote_id')->unsigned(); 
     $table->integer('local_id')->unsigned(); 
     $table->string('local_type'); 
     $table->timestamps(); 

     $table->foreign('local_id')->references('id')->on('contents')->onDelete('cascade'); 
    }); 

これは、コンテンツテーブルです。これは、すべての記事のインポートです:私はすべてを輸入していたときに

public function all() 
{ 
    $include  = array_diff($this->indexable, ['folder']); 
    $publishedPostsIDs = $this->import($include); 
    $this->deleteOldContent($publishedPostsIDs); 
} 

private function import($include) 
{ 
    $posts    = Post::where('post_status', 'publish')->whereIn('post_type', $include)->get(); 
    foreach ($posts as $post) { 
     $publishedPostsIDs[] = $post->ID; 
     $this->contentInterface->updateOrCreateInventory($post->ID); 
    } 

    return $publishedPostsIDs; 
} 

public function updateOrCreateInventory($remoteId) 
{ 
    $this->contentInterface->updateOrCreateInventory($remoteId); 
} 

private function deleteOldContent($publishedPostsIDs) 
{ 
    $contentToDelete = Content::whereNotIn('cms_id', $publishedPostsIDs)->get(); 

    if (count($contentToDelete) > 0) { 
     foreach ($contentToDelete as $content) { 
      $content->delete(); 
     } 
    } 
} 

だから、私はちょうどall機能へと進むルートに移動し、ポストタイプのフォルダではないリモートWPのDBからのすべての投稿インポートされます。リモートDBから1つのポストをインポートする場合は、updateOrCreateInventory機能に直接行くルートがあります。私はまた、基本的に関数allとほぼ同じタイプのフォルダの投稿のインポートを持っています。

public function folder() 
{ 
    $include = ['folder']; 
    $importResult = $this->import($include); 

    return $importResult['imported']; 
} 

私が持っている問題は、私は、私はエラーを取得し、一度にすべてのフォルダをインポートしていたときにということである。

QueryException in Connection.php line 729: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (middleton . inventories , CONSTRAINT inventories_local_id_foreign FOREIGN KEY (local_id) REFERENCES contents (id) ON DELETE CASCADE) (SQL: insert into inventories (remote_id , updated_at , created_at) values (7512, 2017-11-13 15:33:17, 2017-11-13 15:33:17))

しかし、私は個別に、同じフォルダをインポートする、またはより正確にしようとした場合同じタイプのフォルダの投稿、私はエラーを取得しないと、投稿がインポートされます。そんなことがあるものか?

答えて

0

$ this-> contentInterfaceは、 "local_id"フィールドが指定されていないインベントリモデルを作成しますが、外部キーで必要です。

インベントリモデルを作成し、有効な「local_id」を指定する場所を探します。

関連する問題