was following a few different tutorials trying to learn foreign key migration. I have Laravel v9.
Can't solve this error:
General error: 1005 Can't create table `laravel`.`galleries` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `galleries` add constraint `galleries_album_id_foreign` foreign key (`album_id`) references `albums` (`id`) on delete cascade)
Migration file:
Schema::create('galleries', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('album_id')->nullable();
$table->string('title');
$table->longText('details');
$table->string('image')->nullable();
$table->timestamps();
$table->foreign('album_id')->references('id')->on('albums')
->onDelete('cascade');;
});
Schema::create('albums', function (Blueprint $table) {
$table->id();
$table->string("name");
$table->timestamps();
});
I'd appreciate the help as I have no idea what to do.
I tried: $table->integer('album_id')->unsigned();
which didn't work.
Made sure there's no typo. Everything looks good to me as I was following the tutorial on this.
CodePudding user response:
What worked for me was, I had to change the name of my database schema as to:
Schema::create('galleriesx', function (Blueprint $table) {
Works now. Have no idea why this happened. Would like some insight if you can help, thanks.
CodePudding user response:
$table->bigInteger("album_id")->unsigned();
$table->foreign('album_id')
->references('id')
->on('albums')
->onDelete('cascade')
->onUpdate('cascade');