mafortis
mafortis

Reputation: 7128

laravel migration SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

When I try to run migration command in laravel i get this error

 Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `posts` add constraint `posts_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade)

Before you mark this question as duplicated or vote-down please read the question fully.

The solutions I've found on this site was:

  1. Mark as unsigned I have that
  2. Make integer in 2 step I have that
  3. Schema order issue I provide screenshot to see that's not the case

one

I'm not sure why I get that error, here is my code:

Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('slug')->unique();
            $table->longText('body');
            $table->string('photo');
            $table->text('meta_description')->nullable();
            $table->text('meta_tags')->nullable();
            $table->integer('user_id')->unsigned();
            $table->string('publish')->default('0');
            $table->string('comment')->default('0');
            $table->timestamps();
});
Schema::table('posts', function (Blueprint $table) {
  $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});

Any idea?

Update

Users schema

Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('photo')->nullable();
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
});

Upvotes: 1

Views: 99

Answers (2)

Vishal Ribdiya
Vishal Ribdiya

Reputation: 880

Just replace this code ::

Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('slug')->unique();
            $table->longText('body');
            $table->string('photo');
            $table->text('meta_description')->nullable();
            $table->text('meta_tags')->nullable();
            $table->bigInteger('user_id')->unsigned();
            $table->string('publish')->default('0');
            $table->string('comment')->default('0');
            $table->timestamps();

            $table->foreign('user_id')
               ->references('id')
               ->on('users')
               ->onDelete('cascade');

});

Upvotes: 2

Ahmed Nour Jamal El-Din
Ahmed Nour Jamal El-Din

Reputation: 1011

The primary key in users table is BigIncrements which create an unsigned big integer column, but the foreign key in posts table is integer, so they are not the same type.

Changing the foreign key to bigInteger will fix it.

so this:

$table->bigInteger('user_id')->unsigned();

instead of:

$table->integer('user_id')->unsigned();

Upvotes: 2

Related Questions