5

I've been told to remove the auto incrementing functionality of one of our tables, however this started to cause problems:

PDOException::("SQLSTATE[HY000]: General error: 3780 Referencing column 'limitCardId' and referenced column 'id' in foreign key constraint 'rolling_control_cards_limitcardid_foreign' are incompatible.")

Here are the keys in question:

PK $table->integer('id');

FK $table->integer('limitCardId')->unsigned()->index();

Relation $table->foreign('limitCardId')->references('id')->on('limit_cards');

I've read several other posts that suggested several solutions, however none solved my issue, here's a list of what i've already tried:

Defining this in my base table model:

    protected $primaryKey = 'id';
    public $incrementing = false;

Adding Schema::disableForeignKeyConstraints(); at the start of the up() method, and Schema::enableForeignKeyConstraints(); at the end, i've ran composer dump-autoload, php artisan config:clear, php artisan cache:clear.

As suggested, here's the stripped version of both tables, Base table:

 public function up()
    {
        Schema::create('limit_cards', function (Blueprint $table) {
            $table->integer('id');
            $table->timestamps();
        });
    }

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

Related table:

    public function up()
    {
        Schema::create('rolling_control_cards', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('limitCardId')->unsigned()->index();
            $table->unique(['controlCardNumber', 'limitCardId']);
            $table->timestamps();

            $table->foreign('limitCardId')->references('id')->on('limit_cards');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('rolling_control_cards');
    }
10
  • Are you running the migration on a fresh database? Commented Feb 6, 2020 at 14:43
  • php artisan migrate:fresh --seed, Does that qualify as a fresh DB? Otherwise no, the DB has records atm. Commented Feb 6, 2020 at 14:44
  • Dropped all tables, still nothing. Thanks for the suggestion tho. Commented Feb 6, 2020 at 14:46
  • Yes, this command will drop all the tables and recreate them. Can you post the full migration of both limit_cards and rolling_control_cards tables? Commented Feb 6, 2020 at 14:46
  • @mdexp I'm not very confortable to post the migrations here (and it will get bloated), do you mind if post them in a chat with you? Commented Feb 6, 2020 at 14:49

3 Answers 3

9

In order for foreign key references to be compatible they must be of the exact same type, including their signedness.

$table->integer('id'); creates a signed integer column by default. Either make it unsigned to match limitCardId or make them both signed.

Sign up to request clarification or add additional context in comments.

Comments

3

limit_cards

public function up()
{
    Schema::create('limit_cards', function (Blueprint $table) {
        $table->unsignedInteger('id');
        $table->timestamps();
    });
}

unsignedInteger => UNSIGNED INT equivalent column.

rolling_control_cards

Schema::table('rolling_control_cards', function (Blueprint $table) {
    $table->increments('id');
    $table->unsignedInteger('limitCardId')->nullable();
    $table->unique(['controlCardNumber', 'limitCardId'])
    $table->timestamps();

    $table->foreign('limitCardId')->references('id')->on('limit_cards');
});

3 Comments

What i did to solve the issue by danronmoon suggestion is - I've made both keys ->unsigned()->index();
@Y.Futekov No problem. This will be a reference for other visitors.
@ Wahyu Kristianto The idea is for the PK of limit_cards to no longer be increments, just integer.
0

Warning : Making "unsigned" will cause future errors if you want to switch to another RDBMS since "UNSIGNED" is for SQL only

Schema::create('rolling_control_cards', function (Blueprint $table) {
    $table->unsignedBigInteger('limitCardId');

    $table->foreign('limitCardId')->references('id')->on('limit_cards');
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.