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

hjortur17's avatar

Not able to add foreign key

Hi, I'm trying to add a pivot table and have foreign keys in it but I always get this error:

SQLSTATE[HY000]: General error: 3780 Referencing column 'booking_id' and referenced column 'id' in foreign key constraint 'booking_service_booking_id_foreign' are incompatible. (SQL: alter table `booking_service` add constraint `booking_service_booking_id_foreign` foreign key (`booking_id`) references `bookings` (`id`) on delete cascade)

Here is my CreateBookingServiceTable:

public function up()
    {
        Schema::create('booking_service', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('booking_id');
            $table->unsignedInteger('service_id');
            $table->timestamps();

            $table->unique(['booking_id', 'service_id']);
        });

        Schema::table('booking_service', function($table) {
            $table->foreign('booking_id')->references('id')->on('bookings')->onDelete('cascade');
            $table->foreign('service_id')->references('id')->on('services')->onDelete('cascade');
        });
    }

And here is my CreateBookingsTable

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

And here is my CreateServicesTable

public function up()
    {
        Schema::create('services', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('carSize');
            $table->string('description');
            $table->timestamps();
        });
    }
0 likes
2 replies
Nakov's avatar
Nakov
Best Answer
Level 73

@hjortur17

Change this

$table->unsignedInteger('booking_id');

to

$table->unsignedBigInteger('booking_id');

the type of bigIncrements is a Big Integer.

And also this:

$table->unsignedInteger('service_id');

should be:

$table->unsignedBigInteger('service_id');
1 like
skauk's avatar

@hjortur17 The columns have to be the same type, so if you're referencing the id column that is BIGINT then the foreign key column has to be BIGINT as well.

1 like

Please or to participate in this conversation.