2017-04-18 4 views
0

職人を実行しているとき、私はエラーを取得するLARAVELの移行ERROR SQLSTATE [42000]「テーブルに存在する」

最初の移行Iは多対多の関係のために実行し、多くの関係にテーブルを追加しますさ何度も何度も端末にエラーが表示される場合は、移行したときに何が起こったのでしょうか。

[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

関係に欠けているあなたは、あなたの第二に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

おかげで、それは動作します:) –

関連する問題