首页 > 解决方案 > 外键约束不兼容?

问题描述

在重新处理一些表逻辑后试图让我的迁移工作,但我在迁移时遇到了问题。与约束冲突有关。

运行php artisan migrate:fresh时显示这个,

In Connection.php line 665:

  SQLSTATE[HY000]: General error: 3780 Referencing column 'cache_id' and referenced column 'id' in
   foreign key constraint 'cache_connections_cache_id_foreign' are incompatible. (SQL: alter table
   `cache_connections` add constraint `cache_connections_cache_id_foreign` foreign key (`cache_id`
  ) references `cache` (`id`))


In Connection.php line 459:

  SQLSTATE[HY000]: General error: 3780 Referencing column 'cache_id' and referenced column 'id' in
   foreign key constraint 'cache_connections_cache_id_foreign' are incompatible.

迁移:

Schema::create('cache', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('item')->unique();
    $table->string('name');
    $table->string('picture');
    $table->string('description');
    $table->unsignedInteger('connection_count');
    $table->boolean('is_private');
    $table->json('additional_data')->nullable();
    $table->string('type_id');
    $table->timestamps();
});

Schema::create('cache_connections', function (Blueprint $table) {
    $table->bigInteger('cache_id');
    $table->string('connection');
    $table->timestamps();

    $table->foreign('cache_id')
        ->references('id')
        ->on('cache')
    ;
});

标签: phplaravel

解决方案


这可能是由于bigIncrements()使用unsignedBigInteger(),而不是bigInteger()。尝试更新您的cache_connections迁移以使用未签名的方法。

供应商/laravel/framework/src/Illuminate/Database/Schema/Blueprint.php

    /**
     * Create a new auto-incrementing big integer (8-byte) column on the table.
     *
     * @param  string  $column
     * @return \Illuminate\Database\Schema\ColumnDefinition
     */
    public function bigIncrements($column)
    {
        return $this->unsignedBigInteger($column, true);
    }

推荐阅读