【问题标题】:laravel migration error: Can't create table Foreign key constraint is incorrectly formedlaravel 迁移错误:无法创建表外键约束的格式不正确
【发布时间】: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 "外键约束的格式不正确")

这是涉及的两个迁移:

  1. 对于父表
    <?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');
        }
    }
    
    1. 对于包含外键的表
    <?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');
        }
    }
    
    

    这里还有表格的截图:

    1. 父表

      1. 包含表的外键

【问题讨论】:

  • 列需要完全匹配,所以 unsigned integer 必须匹配 unsigned integer,unsigned bigInteger 需要匹配 unsigned bigInteger
  • 上图中的 @aynber 在迁移运行后确实匹配。有什么我想念的吗?
  • 错误信息还有更多内容吗?
  • @aynber 我刚刚在顶部编辑了问题以包含错误消息,希望能有所帮助!
  • 嗯,我很茫然。列是相同的,如果它们都是用 Laravel 创建的,我希望排序规则是相同的。

标签: php mysql laravel database


【解决方案1】:

问题是由于下面列出的外键创建限制,父表 client_department_service 上的列 fifteenfive_group_id 必须是唯一的:

FOREIGN KEY 约束不必仅链接到另一个表中的 PRIMARY KEY 约束;它也可以定义为引用另一个表中 UNIQUE 约束的列。

【讨论】:

    猜你喜欢
    • 2020-11-12
    • 2021-12-24
    • 2021-04-06
    • 2017-11-12
    • 2019-07-10
    • 2019-07-27
    • 2018-05-23
    • 2015-12-16
    • 2019-09-10
    相关资源
    最近更新 更多