2017-05-06 1 views
0

laravel에서 테이블 '팀'과 '대회'를 만들려고하지만 migrate 명령을 실행할 때 다음과 같은 결과가 나옵니다. errno : 150 "외래 키 제약 조건이 잘못 구성되었습니다. "Laravel - 외래 키 제약 조건이 잘못 형성되었습니다

Schema::create('competitions', function (Blueprint $table) { 
        $table->increments('id'); 
        $table->string('name')->unique(); 
        $table->string('team_name'); 
        $table->foreign('team_name')->references('name')->on('teams')->onDelete('cascade'); 
        $table->timestamps(); 
     }); 

Schema::create('teams', function (Blueprint $table) { 
        $table->increments('id'); 
        $table->string('name')->unique(); 
        $table->string('place'); 
        $table->string('competition')->nullable();; 
        $table->foreign('competition')->references('name')->on('competitions')->onDelete('set null'); 
        $table->timestamps(); 
}); 

답변

0

나는 이것이 귀하의 ->onDelete('set null') 조항 때문이라고 생각합니다. 이렇게하면 팀을 삭제할 때 경쟁 테이블의 이름 필드가 null로 설정됩니다. 아마 이것을 제거 할 수 있습니다.

+0

아니요, 여전히 동일한 오류가 발생하지 않았습니다. – jordibenck

0

당신은 기본 키외래 키

예에 대한 동일한 속성을 사용해야합니다. 둘 다 -> nullable() 또는 없습니다.

또한 두 테이블과 해당 열의 데이터 정렬이 동일한 지 확인하십시오.

관련 문제