2016-07-21 93 views
0

主表民意調查PHP工匠遷移外鍵錯誤

public function up() 
    { 
     Schema::create('poll', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->timestamps(); 
      $table->string('poll_question', 255); 
      $table->boolean('status',1)->default(0)->index(); 
      $table->unsignedInteger('order'); 
     }); 
    } 

詳細信息表

public function up() 
    { 
     Schema::create('poll_option', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->timestamps(); 
      $table->integer('poll_id'); 
      $table->string('poll_option', 255); 
      $table->unsignedInteger('vote_poll_option'); 
      $table->unsignedInteger('order'); 
      $table->boolean('status',1)->default(0)->index(); 

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

當我運行PHP的工匠與外鍵

SQLSTATE[HY000]: General error: 1005 Can't create table mydb . #sql-6f4_433 (errno: 150 "Foreign key constraint is incorrectly formed")

注:我使用laravel 5.2和MySQL類型已經InnoDB的什麼是主要原因不正確地形成

+0

可能$表 - >整數( 'poll_id') - >無符號(); –

+1

或$ table-> unsignedInteger('poll_id') – ClearBoth

回答

0

在明細表

public function up() 
    { 
     Schema::create('poll_option', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->timestamps(); 
      $table->integer('poll_id')->unsigned(); //Change Here 
      $table->string('poll_option', 255); 
      $table->unsignedInteger('vote_poll_option'); 
      $table->unsignedInteger('order'); 
      $table->boolean('status',1)->default(0)->index(); 

      $table->foreign('poll_id') 
       ->references('id')->on('poll') 
       ->onDelete('CASCADE') 
       ->onUpdate('CASCADE'); 
     }); 
    } 

這會爲你工作

+0

謝謝Bindesh。我在外鍵字段中錯過了「 - > unsigned()」。 –

+0

如果你喜歡,你可以接受我的答案 –