2017-04-18 1 views
0

나는LARAVEL 마이그레이션 오류 SQLSTATE [42000] 장인을 실행할 때 "테이블에 존재"

처음에는 관계에 대한 실행 대다과의 관계에 많은에게로 테이블을 추가 할 수있다 마이그레이션 오류가 발생합니다 -to-many 및 마이그레이션 할 때 발생하는 오류는 터미널에 표시됩니다.

[Illuminate\Database\QueryException]           
    SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'hobi' d 
    oesn't exist in table (SQL: alter table `hobi_siswa` add constraint `hobi_s 
    iswa_hobi_foreign` foreign key (`hobi`) references `hobi` (`id`) on delete 
    cascade on update cascade)             



    [PDOException]                
    SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'hobi' d 
    oesn't exist in table    

가 터미널했다,하지만 난 놀랐고 그 목적이 키 관계 수이다 동등하게 padahah 이유에 혼란스러워했다.

<?php 

use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateTableHobiSiswa extends Migration 
{ 
    public function up() 
    { 
     //create table hobi siswa 
     Schema::create('hobi_siswa', function (Blueprint $table) { 
      $table->integer('id_siswa')->unsigned()->index(); 
      $table->integer('id_hobi')->unsigned()->index(); 
      $table->timestamps(); 

      //set PK 
      $table->primary(['id_siswa', 'id_hobi']); 

      //set FK hobi siswa --- siswa 
      $table->foreign('id_siswa') 
        ->references('id') 
        ->on('hobi') 
        ->onDelete('cascade') 
        ->onUpdate('cascade'); 

      //Set FK Hobi_siswa ---hobi 
      $table->foreign('hobi') 
        ->references('id') 
        ->on('hobi') 
        ->onDelete('cascade') 
        ->onUpdate('cascade'); 
     }); 

    } 

    public function down() 
    { 
     Schema::drop('hobi_siswa'); 
    } 
} 

하고 createTableHobiSiswa입니다 내가

<?php 

use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateTableHobi extends Migration 
{ 

    public function up() 
    { 
     Schema::create('hobi', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('nama_hobi'); 
      $table->timestamps(); 
     }); 
    } 

    public function down() 
    { 
     Schema::drop('hobi'); 
    } 
} 

을 마이그레이션 할 일을하고이 createTableHobi

답변

1

관련 원해요 당신이 당신의 2에 foreign()에 준 값처럼 나에게 보이는 외래 키 체인은 테이블의 열이 실제로 id_hobi 일 때 hobi 열을 참조합니다. 이 대신에

//Set FK Hobi_siswa ---hobi 
$table->foreign('hobi') 
     ->references('id') 
     ->on('hobi') 
     ->onDelete('cascade') 
     ->onUpdate('cascade'); 

:

변경이 부분을보십시오

//Set FK Hobi_siswa ---hobi 
$table->foreign('id_hobi') //reference the column on this table correctly 
     ->references('id') 
     ->on('hobi') 
     ->onDelete('cascade') 
     ->onUpdate('cascade'); 
+0

감사 작동 :) –

관련 문제