Sachin
Sachin

Reputation: 83

Laravel migration errno: 150 "Foreign key constraint is incorrectly formed"

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.

Upvotes: 1

Views: 2190

Answers (1)

steven7mwesigwa
steven7mwesigwa

Reputation: 6700

Your 2021_04_03_134426_create_music_uploads_table migration is essentially running before the 2021_11_11_195944_create_albums_table migration.

Solution.

STEP 1

Rename:
2021_04_03_134426_create_music_uploads_table
To:
2021_11_11_195944_create_music_uploads_table

STEP 2

Rename:
2021_11_11_195944_create_albums_table.php
To:
2021_04_03_134426_create_albums_table.php

STEP 3

Re-run the migration(s).

php artisan migrate

NOTES:

Always ensure that tables (migrations) with foreign key columns are created after the tables (migrations) they refer to.

In your case, you created the music_uploads migration on 03th/04/2021. And then created the albums migration on 11th/11/2021. Yet the albums migration had to be created earlier than that of music_uploads.

Migrations are run in the order of their prepended timestamp.

Upvotes: 3

Related Questions