【问题标题】:Laravel 7.4 cannot add foreign key constraintLaravel 7.4 无法添加外键约束
【发布时间】:2021-10-27 20:53:47
【问题描述】:

所以我在我的旧文件的template 之后创建了一个迁移文件,如下所示:

create_banners_table(这个WORKED

class CreateBannersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('banners', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('order');
            $table->string('published');
            $table->timestamps();
        });

        Schema::create('banner_translations', function (Blueprint $table) {
            $table->increments('id')->unsigned();
            $table->string('locale', 10)->index();
            $table->string('title');
            $table->string('url');
            $table->foreignId('banner_id')
                ->constrained()
                ->onDelete('cascade');
        });
    }

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

这是create_managements_table(这个FAILED):

class CreateManagementsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('managements', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('order');
            $table->string('published');
            $table->string('name');
            $table->string('category')
                ->comment('board of commissioners', 'board of executives');
            $table->timestamps();
        });

        Schema::create('management_translations', function (Blueprint $table) {
            $table->increments('id')->unsigned();
            $table->string('locale', 10)->index();
            $table->string('description');
            $table->string('title');
            $table->foreignId('management_id')
                ->constrained()
                ->onDelete('cascade');
        });
    }

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

问题是,当我尝试基于此创建另一个迁移文件时,我收到了这个错误:

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `management_translations` add constraint `management_translations_management_id_foreign` foreign key (`management_id`) references `management` (`id`) on delete cascade)

如您所见,两个转换表都引用了各自基表中的大整数(因此没有类型不匹配)。

为了进一步调查,我跑了SHOW ENGINE INNODB STATUS; 并检查了Latest Foreign Key Error 并得到以下信息:

------------------------
LATEST FOREIGN KEY ERROR
------------------------
2021-08-28 12:01:41 0x7f6f44091700 Error in foreign key constraint of table cemindo/#sql-200_7:
 foreign key (`management_id`) references `management` (`id`) on delete cascade:
Cannot resolve table name close to:
 (`id`) on delete cascade

【问题讨论】:

  • $table->bigIncrements('id');$table->increments('id')->unsigned();改成$table->id();就可以解决你的问题了
  • ...我已经为各自基表 bigIncrement 的两个引用 Id 所做的工作对于 Banner 工作得很好,但对于 Management 却失败了。 .. 想知道为什么
  • 将两者都更改为 $table->id(); 仍然会导致相同的错误
  • 个人建议,千万不要使用MySQL的foreign关系,他们只会给你这些问题。如果您的代码是正确的,您仍然应该删除所有内容,就好像 foreign 存在一样。另外,请使用id(); 而不是increments,因为您没有正确定义它...您必须告诉它是id,并且您使用idbigIncrements 这样做。而且,超级重要,不要使用一次迁移来创建超过 1 个,100% 建议在单独的迁移中创建每个表,即使它们是相关的,你可以在这种情况下回滚每个。

标签: mysql laravel migration laravel-7 laravel-migrations


【解决方案1】:

management 可数这个词是否模棱两可,所以显然 Laravel 在这种情况下将其视为不可数。也就是说,Laravel 认为 management_id 对应的表名应该是 management 而不是 managements,这是一个不常见的复数形式。因此,您应该将基表重命名为 management 或使用以下外键 sytnax:

Schema::create('management_translations', function (Blueprint $table) {    
    $table->increments('id')->unsigned();
    $table->string('locale', 10)->index();
    $table->string('description');
    $table->string('title');
    $table->foreignId('management_id')
        ->constrained('managements')
        ->onDelete('cascade');
});

【讨论】:

    【解决方案2】:
    Schema::create('banner_translations', function (Blueprint $table) {
                $table->increments('banner_id')->unsigned();     //change this line
                $table->string('locale', 10)->index();
                $table->string('title');
                $table->string('url');
                $table->foreignId('banner_id')
                    ->constrained()
                    ->onDelete('cascade');
            });
    

    我认为你应该先提供banner_id,然后提供foreignId。(相同的列名)

    【讨论】:

    • 不...banner 表工作非常好,它是management 表在起作用
    【解决方案3】:

    你可以像下面这样添加外键引用,

    首先创建所有键,然后最后声明外键引用...您可以在Laravel Doc's 看到此方法...请参阅该链接的第一个示例

    Schema::create('managements', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('order');
            $table->string('published');
            $table->string('name');
            $table->string('category')
                ->comment('board of commissioners','board of executives');
            $table->timestamps();
    });
    
    Schema::create('management_translations', function (Blueprint $table) {
            $table->increments('id')->unsigned();
            $table->string('locale', 10)->index();
            $table->string('description');
            $table->string('title');
            $table->unsignedBigInteger('management_id');
            $table->foreign('management_id')
                    ->references('id')
                    ->on('managements');
                    ->onDelete('cascade');
    });
    

    如果您在使用我的解决方案时遇到任何错误,请告诉我,我会更新我的答案

    谢谢

    【讨论】:

      猜你喜欢
      • 2020-09-12
      • 2019-10-26
      • 1970-01-01
      • 2018-11-25
      • 2016-11-11
      • 2017-09-06
      • 2017-03-03
      • 1970-01-01
      • 2022-11-17
      相关资源
      最近更新 更多