blagojkk
blagojkk

Reputation: 21

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

can someone please help me with this one? I'm new to Laravel and when I try to do:

php artisan migrate

I get the error:

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

and this is the post table content:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('author_id')->unsigned();
            $table->foreign('author_id')->references('id')->on('users')->onDelete('restrict');
            $table->string('title');
            $table->string('slug')->unique();
            $table->text('excerpt');
            $table->text('body');
            $table->string('image')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

I have noticed that when I comment this line:

$table->foreign('author_id')->references('id')->on('users')->onDelete('restrict');

the error is gone, so I guess the problem is in that line. So, can someone please tell me what will be the correct syntax or how to fix it?

Upvotes: 0

Views: 724

Answers (2)

Jovs
Jovs

Reputation: 892

Try to change the foreign ko to unsignedInteger

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('author_id');
            $table->foreign('author_id')->references('id')->on('users')->onDelete('restrict');
            $table->string('title');
            $table->string('slug')->unique();
            $table->text('excerpt');
            $table->text('body');
            $table->string('image')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

Upvotes: 0

Chin Leung
Chin Leung

Reputation: 14921

The column data type must be the same on both tables for the foreign key to work. By default, the users id column is:

$table->bigIncrements('id');

Therefore, you need to use bigInteger for your posts migration:

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

Upvotes: 2

Related Questions