Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zersiax's avatar

Referencing column is incompatible when adding foreign keys

I am trying to create a few pretty simple migrations. Haven't used Laravel for a while so I might just be missing something obvious, but:

  • I have a few migrations that create tables, followed by one single migration that adds foreign keys to those migrations to link them together.

  • The id column on the tables is set using bigIncrements('id').

  • The column housing the foreign keys are of type bigInteger(column_name).

  • All table creation migrations run first, so the tables are definitely there by the time the foreign keys should be created. And yet, I get the following error:

    Illuminate\Database\QueryException

    SQLSTATE[HY000]: General error: 3780 Referencing column 'organization_id' and referenced column 'id' in foreign key constraint 'courses_organization_id_foreign' are incompatible. (SQL: alter table courses add constraint courses_organization_id_foreign foreign key (organization_id) references organizations (id) on delete cascade)

Here are the migrations in question:"


<?php

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

class CreateOrganizationsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('organizations', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string("name");
            $table->timestamps();
        });
    }

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


<?php

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

class CreateCoursesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('courses', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string("title");
            $table->text("Description");
            $table->decimal("price");
            $table->enum('category', ['e-learning', 'webinar', 'fysiek', 'congres']);
            $table->bigInteger("organization_id");
            $table->bigInteger("date_id");

            $table->index('organization_id');

            $table->index('date_id');
            $table->timestamps();
        });
    }

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


<?php

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

class AddForeignKeys extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('courses', function(Blueprint $table){
            $table->foreign('organization_id')->references('id')
                ->on('organizations')->onDelete('cascade');
            $table->foreign('date_id')->references('id')
            ->on('dates')->onDelete('cascade');
        });

        Schema::table('dates', function (Blueprint $table) {
            $table->foreign('location_id')->references('id')
                ->on('locations')->onDelete('cascade');
            $table->foreign('course_id')->references('id')
                ->on('courses')->onDelete('cascade');
        });


    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}


I have checked the forums on this issue and it usually seems to be a mismatch between integer vs bigInteger or some such, but that isn't the case here for as far as I can see. Does anyone know what I am missing?

0 likes
5 replies
aurawindsurfing's avatar

@Tray2 good few years now into laravel and I only now realised why I would never use

$table->id();

even again ;-)

zersiax's avatar

Thanks a lot. Changing bigInteger to unsignedBigInteger seems to have done the trick.

Tray2's avatar

Then please mark the reply as best.

1 like
zersiax's avatar

Honestly I'd love to but I don't seem to see a button to do so anywhere? I am using a screen reader, perhaps the control is inaccessible?

Please or to participate in this conversation.