Laravel - 外键的 5.4v 和 5.8v 差异

Laravel - 5.4v and 5.8v differences in foreign key

Laravel 5.4 版本和 Laravel 5.8 版本的外键有何不同?我尝试在我的 5.8 版本 Laravel 项目上安装 devdojo/chatter 并且在迁移时它向我显示外键的各种错误,但是当我在 5.4 版本上尝试它时它工作得很好,根本没有错误.请帮助,我找不到解决方案,我的项目中需要它。谢谢!

这是我的错误: SQLSTATE[HY000]: General error: 1005 Can't create table `cdm`.`chatter_discussion` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `chatter_discussion` add constraint `chatter_discussion_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade on update cascade)

这是我的 chatter_discussion table:

<?php

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

class CreateChatterDiscussionTable extends Migration
{
    public function up()
    {
        Schema::create('chatter_discussion', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('chatter_category_id')->unsigned()->default('1');
            $table->string('title');
            $table->integer('user_id')->unsigned();
            $table->boolean('sticky')->default(false);
            $table->integer('views')->unsigned()->default('0');
            $table->boolean('answered')->default(0);
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('chatter_discussion');
    }
}

这是 foreign_keys table:

<?php

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

class CreateForeignKeys extends Migration
{
    public function up()
    {
        Schema::table('chatter_discussion', function (Blueprint $table) {
            $table->foreign('chatter_category_id')->references('id')->on('chatter_categories')
                        ->onDelete('cascade')
                        ->onUpdate('cascade');
            $table->foreign('user_id')->references('id')->on('users')
                        ->onDelete('cascade')
                        ->onUpdate('cascade');
        });
        Schema::table('chatter_post', function (Blueprint $table) {
            $table->foreign('chatter_discussion_id')->references('id')->on('chatter_discussion')
                        ->onDelete('cascade')
                        ->onUpdate('cascade');
            $table->foreign('user_id')->references('id')->on('users')
                        ->onDelete('cascade')
                        ->onUpdate('cascade');
        });
    }

    public function down()
    {
        Schema::table('chatter_discussion', function (Blueprint $table) {
            $table->dropForeign('chatter_discussion_chatter_category_id_foreign');
            $table->dropForeign('chatter_discussion_user_id_foreign');
        });
        Schema::table('chatter_post', function (Blueprint $table) {
            $table->dropForeign('chatter_post_chatter_discussion_id_foreign');
            $table->dropForeign('chatter_post_user_id_foreign');
        });
    }
}

https://github.com/thedevdojo/chatter/issues/257 描述这个问题。

问题是 boilerplate user migration 主键从 increments 更改为 bigIncrements 因此您需要更改引用:

class CreateChatterDiscussionTable extends Migration
{
    public function up()
    {
        Schema::create('chatter_discussion', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('chatter_category_id')->unsigned()->default('1');
            $table->string('title');
            $table->bigInteger('user_id')->unsigned();
            $table->boolean('sticky')->default(false);
            $table->integer('views')->unsigned()->default('0');
            $table->boolean('answered')->default(0);
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('chatter_discussion');
    }
}

注意:github 问题建议更改用户迁移,但我强烈建议您不要这样做,因为有充分的理由从一开始就更改它。如果你这样做,你可能会没事,但你永远不知道。

在 Laravel 5.4 上,所有 id 列默认为 increments 类型,但在 Laravel 5.8 上默认为 bigIncrements 类型。因此,将 user_id 列类型更改为 bigInteger on chatter_discussion table 将解决您的问题:

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

请记住,您的本地键和外键在迁移时应始终具有相同的类型。