Andreas Hunter
Andreas Hunter

Reputation: 5014

Laravel 6 - MariaDB 10.1: Illuminate\Database\QueryException : SQLSTATE[HY000] migration error

I have custom migration:

Code:

// Groups migration
Schema::create('groups', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('name');
    $table->boolean('status')->default(false);
    $table->timestamps();
});

// Clients migration
Schema::create('clients', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('fullname');
    $table->integer('phone');
    $table->date('birthday')->nullable();
    $table->boolean('can_get_congratulations')->default(false);
    $table->unsignedInteger('group_id')->default(null);
    $table->foreign('group_id')
          ->references('id')
          ->on('groups')
          ->onDelete('cascade');
    $table->boolean('status')->default(true);
    $table->timestamps();
});

When I run this migration file then get error message:

Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table taxisms.#sql-1cc0_65c (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table clients add constraint clients_group_id_foreign foreign key (group_id) references groups (id) on delete cascade)

enter image description here

Where I have an error in my migration code?

Upvotes: 0

Views: 328

Answers (1)

aynber
aynber

Reputation: 23011

The column needs to match on both sides. Since groups.id is an unsigned big integer, group_id will need to be as well. Change

$table->unsignedInteger('group_id') 

to

$table->unsignedBigInteger('group_id')

Upvotes: 1

Related Questions