【发布时间】:2023-02-03 01:21:36
【问题描述】:
我的 Laravel 迁移失败,因为 Foreign key constraint is incorrectly formed。
我多次检查了列的类型和创建的索引,但还没有找到解决方案。
这是我收到的错误消息:
SQLSTATE[HY000]: General error: 1005 Can't create table `command`.`fifteenfive_group_members` (errn
o: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `fifteenfive_group_members
` add constraint `fifteenfive_group_members_fifteenfive_group_id_foreign` foreign key (`fifteenfive
_group_id`) references `client_department_service` (`fifteenfive_group_id`) on delete cascade on up
date cascade)
在 Connection.php 第 501 行:
SQLSTATE[HY000]: 一般错误: 1005 无法创建表 command.fifteenfive_group_members (errn
o: 150 "外键约束的格式不正确")
这是涉及的两个迁移:
- 对于父表
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateClientDepartmentServiceTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { if (!Schema::hasTable('client_department_service')) { Schema::create('client_department_service', function (Blueprint $table) { $table->increments('id'); $table->unsignedInteger('client_id'); $table->unsignedInteger('department_id')->nullable(); $table->unsignedInteger('service_id')->nullable(); $table->unsignedBigInteger('fifteenfive_group_id'); $table->timestamps(); $table->foreign('department_id', 'client_department_service_ibfk_1')->references('id')->on('departments'); $table->foreign('client_id', 'client_department_service_ibfk_2')->references('id')->on('clients'); $table->foreign('service_id', 'client_department_service_ibfk_3')->references('id')->on('service_type'); }); } } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('client_department_service'); } }- 对于包含外键的表
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateFifteenfiveGroupMembersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { if (!Schema::hasTable('fifteenfive_group_members')) { Schema::create('fifteenfive_group_members', function (Blueprint $table) { $table->increments('id'); $table->unsignedBigInteger('fifteenfive_group_id'); $table->unsignedInteger('fifteenfive_group_member_id'); $table->timestamps(); $table->foreign( 'fifteenfive_group_id') ->references('fifteenfive_group_id') ->on('client_department_service') ->onDelete('cascade') ->onUpdate('cascade'); $table->foreign( 'fifteenfive_group_member_id', 'FK_fifteenfive_group_members_fifteenfive_group_member_id' ) ->references('15five_id') ->on('users') ->onDelete('cascade') ->onUpdate('cascade'); }); } } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('fifteenfive_group_members'); } }这里还有表格的截图:
【问题讨论】:
-
列需要完全匹配,所以 unsigned integer 必须匹配 unsigned integer,unsigned bigInteger 需要匹配 unsigned bigInteger
-
上图中的 @aynber 在迁移运行后确实匹配。有什么我想念的吗?
-
错误信息还有更多内容吗?
-
@aynber 我刚刚在顶部编辑了问题以包含错误消息,希望能有所帮助!
-
嗯,我很茫然。列是相同的,如果它们都是用 Laravel 创建的,我希望排序规则是相同的。
标签: php mysql laravel database