2017-11-13 1 views
0

원격 WP 데이터베이스에서 laravel로 로컬 DB로 일부 콘텐츠를 가져옵니다. 테이블 재고 및 내용을 설정했습니다.Laravel - 무결성 제약 조건 위반 : 1452 자식 행을 추가하거나 업데이트 할 수 없습니다.

재고 테이블은 다음과 같다 : I 원격 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에서 단일 게시물을 가져 오려면 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"필드를 지정하지 않고 Inventory 모델을 작성하지만 외부 키가 필요합니다.

인벤토리 모델을 만들고 유효한 "local_id"를 제공하는 위치를 찾으십시오.

관련 문제