2017-05-06 13 views
0

laravelでテーブル 'チーム'と '競争'を作成しようとしていますが、migrateコマンドを実行すると次のようになります: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')句のためだと思います。これにより、チームを削除すると、競合テーブルの名前フィールドがnullに設定されます。おそらくこれを削除するだけです。

+0

いいえ、まだ同じエラーが機能しませんでした – jordibenck

0

あなたは主キー外部キー

などのために同じプロパティを使用する必要があります。には、両方とも - > nullable()かどうかがありません。

また、2つのテーブルとその列の照合順序が同じであることを確認してください。

関連する問題