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

tenzan's avatar

Referencing column 'owner_id' and referenced column 'id' in foreign key constraint 'locations_owner_id_foreign' are incompatible.

Hi,

Full repo here https://github.com/tenzan/laravel-app-01

Please note users table were automatically generated when I was adding authentication.

I'm having this error when running tests, where location should have an owner:

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

Locations table migration:

 {
        Schema::create('locations', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('owner_id');
            $table->string('name');
            $table->text('description');
            $table->timestamps();

            $table ->foreign('owner_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');
        });
    }

Users table migration:

 {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

Regards.

0 likes
6 replies
tykus's avatar
tykus
Best Answer
Level 104

The foreign key must be the same type as the referenced key on the other table - i.e. unsigned big integer

$table->unsignedBigInteger('owner_id');
1 like
tenzan's avatar

@tykus Thanks. Right after I refactored I have now a different error:

 Symfony\Component\Process\Exception\ProcessSignaledException 

  The process has been signaled with signal "11".

  at vendor/symfony/process/Process.php:434
    430▕             usleep(1000);
    431▕         }
    432▕ 
    433▕         if ($this->processInformation['signaled'] && $this->processInformation['termsig'] !== $this->latestSignal) {
  ➜ 434▕             throw new ProcessSignaledException($this);
    435▕         }
    436▕ 
    437▕         return $this->exitcode;
    438▕     }

      +15 vendor frames 
  16  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()
tykus's avatar

@tenzan infinite loop somewhere?

What action do you take that causes this error?

tenzan's avatar

@tykus This command causes the error sail test --filter LocationsTest .

LocationsTest.php :

<?php

namespace Tests\Feature;

use App\Models\Location;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class LocationsTest extends TestCase
{
    use WithFaker, RefreshDatabase;

    /** @test */
    public function a_user_can_create_a_location()
    {
        $this->withoutExceptionHandling();

        $attributes = [
            'name' => $this->faker->sentence,
            'description' => $this->faker->paragraph
        ];

        $this->post('/locations', $attributes)->assertRedirect('/locations');

        $this->assertDatabaseHas('locations', $attributes);

        $this->get('/locations')->assertSee($attributes['name']);
    }

    /** @test */
    public function a_user_can_view_a_location()
    {
        $this->withoutExceptionHandling();

        $location = Location::factory()->create();

        $this->get($location->path())
            ->assertSee($location->name)
            ->assertSee($location->description);
    }

    /** @test */
    public function a_location_requires_a_name()
    {
        $attributes = Location::factory()->raw(['name' => '']);

        $this->post('/locations',$attributes)->assertSessionHasErrors('name');
    }

    /** @test */
    public function a_location_requires_an_owner()
    {
        $this->withoutExceptionHandling();

        $attributes = Location::factory()->raw();

        $this->post('/locations',$attributes)->assertRedirect('login');
    }

}
tykus's avatar

@tenzan any idea which of the four test examples are not working?

Aside, your last test suggests that an authenticated user is required to store a Location, but none of the earlier tests authenticates a User?

    /** @test */
    public function a_user_can_create_a_location()
    {
        $this->withoutExceptionHandling();

		$this->actingAs(User::factory()->create())); // sign in a User!

        $attributes = [
            'name' => $this->faker->sentence,
            'description' => $this->faker->paragraph
        ];

        $this->post('/locations', $attributes)->assertRedirect('/locations');

        $this->assertDatabaseHas('locations', $attributes);

        $this->get('/locations')->assertSee($attributes['name']);
    }

tenzan's avatar

@tykus Hi! I marked your first answer as the solution to my original question. The issue I'm having now is a different story. I will spend some time to figure out. The thing is I have already installed authentication/registration stuff before I started TDD stuff, so something unexpected is happening...

Please or to participate in this conversation.