2017-11-04 1 views
0

처음에는 가난한 영어로 유감입니다.SQLSTATE [23000] : 무결성 제약 조건 위반 : 1452 자식 행을 추가하거나 업데이트 할 수 없습니다. (laravel5)

아래 그림과 같이 내가 MariaDB (이노 디비, laravel5)에서 테이블을 만들었습니다

* 사용자 테이블

Schema::create('users', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('name'); 
     $table->string('email')->unique(); 
     $table->string('password'); 
     $table->rememberToken(); 
     $table->timestamps(); 
    }); 

* 문서 테이블

Schema::create('articles', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('user_id')->unsigned()->index(); 
     $table->string('title'); 
     $table->text('content'); 
     $table->timestamps(); 

     $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade'); 
    }); 

* 태그 테이블

Schema::create('tags', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('name'); 
     $table->string('slug')->index(); 
     $table->timestamps(); 
    }); 

* articl e_tag 테이블

Schema::create('article_tag', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('article_id')->unsigned(); 
     $table->integer('tag_id')->unsigned(); 

     $table->foreign('article_id')->references('id')-> 
     on('articles')->onDelete('cascade'); 
     $table->foreign('tag_id')->references('id')-> 
     on('tags')->onDelete('cascade'); 
    }); 

그래서 article_tag 테이블에 값을 삽입 할 때 내가 얻을 : 나는이 주제에 대한 다른 게시물을 본 적이

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`mmdance`.`article_tag`, CONSTRAINT `article_tag_tag_id_foreign` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`) ON DELETE CASCADE) (SQL: insert into `article_tag` (`article_id`, `tag_id`) values (1, 9)) 

insert into `article_tag` (`article_id`, `tag_id`) values (3, 9); 

* 오류 메시지, 그러나 해결책이 아니 었습니다. 도움을 주셔서 감사합니다.
감사.

+6

그리고 오류에 대해 어떻게 이해하지 못합니까? ID를 삽입하고 있지만 참조 테이블에 참조 값이 없습니다. –

+0

아, 죄송 합니다만, 이해했습니다. – User8392

답변

1

레코드를 만들거나 업데이트하는 스키마 facade를 가져옵니다.

\Schema::disableForeignKeyConstraints(); 
// Your query 
\Schema::enableForeignKeyConstraints(); 
+0

레코드를 만들거나 업데이트 할 때마다이 키를 사용하지 않으려면 외래 키 제약 조건을 가져야합니다. 그건 나쁜 생각이야. –

0

외래 키를 제거하면 잘 진행됩니다. 또는 기본 테이블에 연결하기위한 참조 키 만들기

관련 문제