user5405648
user5405648

Reputation:

Foreign key constraints are incompatible?

Trying to get my migrations to work after reworking some of the table logic and I'm running into an issue when migrating. Something to do with a constraint conflict.

When running php artisan migrate:fresh it shows this,

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.

Migrations:

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

Upvotes: 1

Views: 2779

Answers (1)

matticustard
matticustard

Reputation: 5149

It could be due to the fact that bigIncrements() uses unsignedBigInteger(), not bigInteger(). Try updating your cache_connections migration to use the unsigned method.

vendor/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);
    }

Upvotes: 3

Related Questions