简体   繁体   中英

General error: 1215 Cannot add foreign key constraint in laravel

Doing the migrations I got this error: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table books add constraint books_writer_id_foreign foreign key ( writer_id ) references writers ( id )) I've tried a lot of things but noone looks to work.

2018_02_18_3165165_create_books_table.php

<?php

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

class CreateBooksTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('books', function (Blueprint $table) {
            $table->engine = 'InnoDB';

            $table->increments('id');


            $table->string('name');
            $table->text('description');

            $table->integer('numPages');
            $table->enum('language', ['spanish', 'english']);

            $table->date('wrote_date')->nullable();


            $table->timestamp('created_at')->useCurrent();
            $table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
        });
        Schema::table('books', function (Blueprint $table) {
            $table->integer('writer_id')->unsigned();
            $table->foreign('writer_id')->references('id')->on('writers');
        });
    }

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

2018_02_18_192915_create_writers_table

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

class CreateWritersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('writers', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->text('description');

            $table->string('nationality');
            $table->date('year_date')->nullable();
            $table->date('dead_date')->nullable();

            $table->timestamp('created_at')->useCurrent();
            $table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
        });
    }

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

Edit: The error was because the first migration was the books, and then the writers causing that the error.

Sometimes based on my experience too close timestamps will break the code and will throw an exception because program thinks that writers table is created after books table try to change writers_table timestamp something like:
2018_02_16_31615

使它成为unsignedInteger因为您正在使用increments()

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

change in migration source your_table_name

Schema::create('your_table_name', function (Blueprint $table) {
$table->BigIncrements('id');

to

Schema::create('your_table_name', function (Blueprint $table) {
$table->increments('id');

Move this line to the first closure:

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

Put it right after:

$table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));

Also, you need to add timestamps to both migration files so Laravel first create the writers table and then the books table:

2018_02_01_000000_create_writers_table.php
2018_02_02_000000_create_books_table.php

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