简体   繁体   中英

Laravel 8 : Unable to create foreign key - “General error: 1215 Cannot add foreign key constraint”

I have two migrations.

2021_07_21_115002_create_resources_table.php

public function up()
{
    Schema::create('resources', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('title', 200);
        $table->smallInteger('issue_year');
        $table->unsignedInteger('publisher_id')->nullable();
        $table->timestamps();
    });

    Schema::table('resources', function (Blueprint $table) {
        $table->foreign('publisher_id')
        ->references('id')
        ->on('publishers');
    });
}

and

2021_07_21_115313_create_publishers_table.php

public function up()
{
    Schema::create('publishers', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name', 200);
        $table->timestamps();
    });
}

It keeps on failing.

   Illuminate\Database\QueryException 

  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `resources` add constraint `resources_publisher_id_foreign` foreign key (`publisher_id`) references `publishers` (`id`))

  at D:\wp-local\ignou\vendor\laravel\framework\src\Illuminate\Database\Connection.php:692
    688▕         // If an exception occurs when attempting to run a query, we'll format the error
    689▕         // message to include the bindings with SQL, which will make this exception a
    690▕         // lot more helpful to the developer instead of just the database's errors.
    691▕         catch (Exception $e) {
  ➜ 692▕             throw new QueryException(
    693▕                 $query, $this->prepareBindings($bindings), $e
    694▕             );
    695▕         }
    696▕     }

  1   D:\wp-local\ignou\vendor\laravel\framework\src\Illuminate\Database\Connection.php:485
      PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint")

  2   D:\wp-local\ignou\vendor\laravel\framework\src\Illuminate\Database\Connection.php:485
      PDOStatement::execute()

I have found over 8 to 10 answers for this particular problem and none seems to be working.

You are trying to add a foreign key in the first migration( 2021_07_21_115002_create_resources_table ):

$table->foreign('publisher_id')
        ->references('id')
        ->on('publishers');

that references the table publishers that does not yet exist because it will be setup in the following migration( 2021_07_21_115313_create_publishers_table.php ).

There are two ways to fix this:

  1. Move the foreign key statement to the latest migration:
// 2021_07_21_115313_create_publishers_table.php

public function up()
{
    Schema::create('publishers', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name', 200);
        $table->timestamps();
    });

    Schema::table('resources', function (Blueprint $table) {
        $table->foreign('publisher_id')
        ->references('id')
        ->on('publishers');
    });
}
  1. Move the Schema::create('publishers') to the first migration:
// 2021_07_21_115002_create_resources_table.php

public function up()
{
    Schema::create('resources', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('title', 200);
        $table->smallInteger('issue_year');
        $table->unsignedInteger('publisher_id')->nullable();
        $table->timestamps();
    });

    Schema::create('publishers', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name', 200);
        $table->timestamps();
    });

    Schema::table('resources', function (Blueprint $table) {
        $table->foreign('publisher_id')
        ->references('id')
        ->on('publishers');
    });
}

Keep in mind which migrations are already applied because these cannot be modified without performing a database reset.

first of all , change code from

$table->unsignedInteger('publisher_id')->nullable();

to

$table->unsignedBigInteger('publisher_id');

, because of latest laravel 8 documenation :enter link description here

,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM