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 tableclients
add constraintclients_group_id_foreign
foreign key (group_id
) referencesgroups
(id
) on delete cascade)
Where I have an error in my migration code?
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')
See this Blog Site. I hope that this article helpful to you. Also, know another topic on this site.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.