2017-04-25 128 views
0

我猜想的常見問題,但我無法解決它,儘管我在網上找到的信息。雄辯 - 無法添加外鍵約束

我有關係的hasMany的序列:

  • 用戶有許多客戶有許多合同有許多材料

我做了我最好的,但我發現自己面對這個錯誤:

[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: create table clients (id int unsigned not null, user_id int unsigne d null, ...) default character set utf8mb4 collate
utf8mb4_unicode_ci engine = InnoDB)

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

遷移:

class CreateUsersTable extends Migration 
{ 
    public function up() 
    { 
     Schema::create('users', function (Blueprint $table) { 
      $table->engine = 'InnoDB'; 

      // Keys 

      $table->increments('id'); 

      // Other 

      ... 
      $table->timestamps(); 
     }); 
    } 

    public function down() 
    { 
     Schema::disableForeignKeyConstraints(); 
     Schema::dropIfExists('clients'); 
     Schema::drop('users'); 
     Schema::enableForeignKeyConstraints(); 
    } 
} 

class CreateClientsTable extends Migration 
{ 
    public function up() 
    { 
     Schema::create('clients', function (Blueprint $table) { 
      $table->engine = 'InnoDB'; 

      // keys 

      $table->unsignedInteger('id')->unique(); 
      $table->primary('id'); 

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

      // others 
      ... 
     }); 
    } 

    public function down() 
    { 
     Schema::disableForeignKeyConstraints(); 
     Schema::dropIfExists('contracts'); 
     Schema::drop('clients'); 
     Schema::enableForeignKeyConstraints(); 
    } 
} 

class CreateContractsTable extends Migration 
{ 
    public function up() 
    { 
     Schema::create('contracts', function (Blueprint $table) { 
      $table->engine = 'InnoDB'; 

      // Keys 

      $table->increments('id'); 
      $table->string('contract_number')->unique(); 

      $table->unsignedInteger('client_id'); 
      $table->foreign('client_id')->references('id')->on('clients') 
        ->onDelete('cascade'); 


      // Others 

      ... 
     }); 
    } 

    public function down() 
    { 
     Schema::disableForeignKeyConstraints(); 
     Schema::dropIfExists('materials'); 
     Schema::drop('contracts'); 
     Schema::enableForeignKeyConstraints(); 
    } 
} 

class CreateMaterialsTable extends Migration 
{ 
    public function up() 
    { 
     Schema::create('materials', function (Blueprint $table) { 
      $table->engine = 'InnoDB'; 

      // Keys 

      $table->increments('id'); 

      $table->string('contract_number')->unique(); 

      $table->unsignedInteger('contract_id'); 
      $table->foreign('contract_id')->references('id')->on('contracts') 
        ->onDelete('cascade'); 

      // Others 

      ... 
     }); 
    } 

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

我在做什麼錯?

+0

你使用的是mysql/mariadb嗎?如果是,什麼版本? – Jerodev

+0

你的列是同一種數據類型嗎? –

+0

我正在使用mysql,是的他們是... – Pixeuh

回答

0

您應該將->unsigned()添加到您擁有的所有關鍵列中。 請使用此代替unsignedInteger()並將類型設置爲integer()

+0

只是做,但顯然不會改變任何... – Pixeuh

0

您確定您的遷移按正確的順序運行嗎?它們應該按照您在示例代碼中提供的順序運行。我似乎無法使用您的確切示例遷移重新創建我的最終錯誤。

訂單取決於遷移文件名開頭的日期。或者,您可以在同一遷移中創建所有這些表以強制執行該順序。

+0

是的,我已經這樣做.... – Pixeuh