I have a music_upload table which references both album_id
and user_id
from both album and user table. The user_id
foreign key works fine, it is the album_id
foreign key which spews out this error
SQLSTATE[HY000]: General error: 1005 Can't create table `nightingalev2`.`music_uploads` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `music_uploads` add constraint `music_uploads_album_id_foreign` foreign key (`album_id`) references `albums` (`id`) on delete cascade)
This is my album
table schema
Schema::create('albums', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('artist_id');
$table->string('name');
$table->timestamps();
$table->foreign('artist_id')->references('id')->on('artists');
});
This is my music_uploads
schema
Schema::create('music_uploads', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('album_id');
$table->string('filename');
$table->string('extension');
$table->string('artistname')->nullable();
$table->string('albumname')->nullable();
$table->string('playtime')->nullable();
$table->string('genres')->nullable();
$table->integer('length')->nullable();
$table->string('filesize')->nullable();
$table->string('location')->nullable();
$table->string('thumbnail')->nullable();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('album_id')->references('id')->on('albums')->onDelete('cascade');
});
And this is my users
table schema if needed
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
I changed from $table->id()
to $table->increment('id')
and it didn't work. Changed the unsignedBitInteger
into integer()->unsigned()
and still didn't work.