Home > front end >  (errno: 150 "Foreign key constraint is incorrectly formed") in laravel 9 migration
(errno: 150 "Foreign key constraint is incorrectly formed") in laravel 9 migration

Time:10-16

I have two tables in my laravel application.

company table employee table.

Following is my company table migration file.

<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('companies', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('logo');
            $table->text('email', 50)->unique();
            $table->string('website');
            $table->timestamps();
        });
    }

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

and the following is the employee table.

<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('employees', function (Blueprint $table) {
            $table->id();
            $table->string('first_name');
            $table->string('last_name');
            $table->text('email', 50)->unique();
            $table->text('phone', 50)->unique();
            $table->integer('company_id')->unsigned();
            $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
            $table->timestamps();
        });
    }

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

But,

Whenever I tried to run my employee table migration, it kept giving me an error saying,

SQLSTATE[HY000]: General error: 1005 Can't create table laraapp.employees (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table employees add constraint employees_company_id_foreign foreign key (company_id) references companies (id) on delete cascade)

ID of my company table is UNSIGNED.

CodePudding user response:

The value of $table->id(); is actually unsignedBigInteger() but you've defined your company_id foreign key column to only be an unsignedInteger so the two column types do no match.

As you are using Laravel 9, you can use the foreignId() method which will automatically create the column with the correct type:

$table->foreignId('company_id');

You could also use the foreignIdFor() method which will also create the underly required fk constraint so you don't have to.

$table->foreignIdFor(Company::class);

For completeness:

<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('employees', function (Blueprint $table) {
            $table->id();
            $table->string('first_name');
            $table->string('last_name');
            $table->text('email', 50)->unique();
            $table->text('phone', 50)->unique();
            $table->foreignIdFor(Company::class);
            $table->timestamps();
        });
    }

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