In my Laravel 8.x project I have this table structure:
plays
+--------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | | |
sceneries
+------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
play_scenery
+------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------------+------+-----+---------+-------+
| play_id | bigint(20) unsigned | NO | MUL | NULL | |
| scenery_id | bigint(20) unsigned | NO | | NULL | |
In my migration file I want to add foreign keys in this way:
Schema::table('play_scenery', function (Blueprint $table) {
$table->foreign('play_id')
->references('id')->on('plays')
->onUpdate('cascade')
->onDelete('cascade');
$table->foreign('scenery_id')
->references('id')->on('scenerys')
->onUpdate('cascade')
->onDelete('cascade');
});
And it fails with this message:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key
constraint (SQL: alter table `play_scenery` add constraint `play_scenery_scenery_id_foreign` foreign key (`scenery_id`)
references `scenerys` (`id`) on delete cascade on update cascade)
I checked this but not helped any of these answers.
Any idea what do I wrong?