I am trying to create a pivot table, with foreign keys, this is the migration I have made in Laravel:
public function up()
{
Schema::create('player_position', function (Blueprint $table) {
$table->integer('player_id')->unsigned()->index();
$table->integer('position_id')->unsigned()->index();
$table->foreign('player_id')->references('id')->on('players')->onDelete('cascade');
$table->foreign('position_id')->references('id')->on('positions')->onDelete('cascade');
});
}
But, I get an error:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter tableplayer_position
add constraintplayer_position_posi tion_id_foreign
foreign key (position_id
) referencespositions
(id
) on delete cascade)[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
I have read that usually foreign key constraint errors are about not assigning unsigned to fields, or already having records in the DB, but my DB is empty, and I my fields have unsigned, so don't know what is the problem?