General error: 1215 Cannot add foreign key constraint in laravel

ⅰ亾dé卋堺 提交于 2019-12-24 08:30:45

问题


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.


回答1:


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




回答2:


Make it unsignedInteger because you're using increments()

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



回答3:


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


来源:https://stackoverflow.com/questions/48856136/general-error-1215-cannot-add-foreign-key-constraint-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!