0
votes

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?

1
It's cascade, not casecadeaynber
@aynber I change to cascade but also get error. See to updated questionAndreas Hunter
Can you show your groups migration as well?aynber
@aynber see to updated questionAndreas Hunter
If I remember correctly, the column definitions need 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')aynber

1 Answers

1
votes

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')