【问题标题】:General error: 1215 Cannot add foreign key constraint in laravel一般错误:1215无法在laravel中添加外键约束
【发布时间】:2018-07-29 02:18:21
【问题描述】:

进行迁移时出现此错误: SQLSTATE[HY000]:一般错误:1215 无法添加外键约束(SQL:更改表books添加约束books_writer_id_foreign外键(writer_id)引用writers(id)) 我尝试了很多东西,但没有一个看起来有效。

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');
    }
}

编辑:错误是因为第一次迁移是书籍,然后是导致错误的作者。

【问题讨论】:

标签: php mysql laravel migration


【解决方案1】:

设为unsignedInteger,因为您使用的是increments()

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

【讨论】:

    【解决方案2】:

    将此行移至第一个闭包:

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

    把它放在后面:

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

    此外,您需要为两个迁移文件添加时间戳,因此 Laravel 首先创建 writers 表,然后创建 books 表:

    2018_02_01_000000_create_writers_table.php
    2018_02_02_000000_create_books_table.php
    

    【讨论】:

      【解决方案3】:

      根据我的经验,有时时间戳太接近会破坏代码并引发异常,因为程序认为在书表尝试更改 writers_table 时间戳之后创建了 writers 表:
      2018_02_16_31615

      【讨论】:

        【解决方案4】:

        迁移源 your_table_name 的更改

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

        到

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

        【讨论】:

          猜你喜欢
          • 2020-06-12
          • 2019-12-27
          • 1970-01-01
          • 2019-09-08
          • 2021-03-29
          • 2018-07-29
          • 1970-01-01
          • 2019-11-11
          • 2019-08-02
          相关资源
          最近更新 更多