2017-05-06 50 views
0

我想在laravel中創建表'隊'和'比賽',但是當我運行遷移命令時,出現以下錯誤: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')子句。當您刪除團隊時,這將嘗試並將比賽桌上的名稱字段設置爲空。你可能會刪除這個。

+0

nope沒有工作,仍然是錯誤 – jordibenck

0

必須使用相同的屬性主鍵外鍵

如。都有 - >可空()或不。

此外,請確保兩個表及其列的排序規則相同。