【问题标题】:How to drop a composite key which includes a foreign key in laravel 5.2 migration如何在 laravel 5.2 迁移中删除包含外键的复合键
【发布时间】:2016-12-16 11:37:55
【问题描述】:

我有一个表(名为j_stones),由 5 个字段组成:

id(primary key)  
j_stones_type(foreign-key)  
shape  
size  
carat

我想让每一行都独一无二,所以我创建了一个迁移来做到这一点:

public function up()
    {
        Schema::table('j_stones', function (Blueprint $table) {
            $table->unique(['j_stone_types_id','shape','size','carat']);
        });
    }

这很好用,但是当我尝试回滚时,我收到以下错误:

1[照亮\数据库\查询异常]

SQLSTATE[HY000]:一般错误:1553 无法删除索引

'j_stones_j_stone_types_id_shape_size_carat_unique':需要 外键约束(SQL:alter table j_stones drop index j_stones_j_stone_types_id_shape_size_carat_unique)

[PDOException] SQLSTATE[HY000]:一般错误:1553 无法删除 索引“j_stones_j_stone_types_id_shape_size_carat_unique”:需要 外键约束'

这是我的回滚代码:

    public function down()
{
    Schema::table('j_stones', function (Blueprint $table) {
        $table->dropUnique(['j_stone_types_id','shape','size','carat']);
    });
}

我尝试像这样禁用外键约束:

    public function down()
{
    DB::statement('SET FOREIGN_KEY_CHECKS = 0'); 
    Schema::table('j_stones', function (Blueprint $table) {

        $table->dropUnique(['j_stone_types_id','shape','size','carat']);
    });
    DB::statement('SET FOREIGN_KEY_CHECKS = 1'); 
}

还有这样的:

    public function down()
{
    Schema::disableForeignKeyConstraints();
    Schema::table('j_stones', function (Blueprint $table) {

        $table->dropUnique(['j_stone_types_id','shape','size','carat']);
    });
    Schema::enableForeignKeyConstraints();
}

但在回滚时仍然不断发生错误。
我将 MySql 与 InnoDb 一起使用。

请指教。

编辑:
我使用以下 hack 进行了回滚,但仍在寻找正确的解决方案:

    public function down()
{

    Schema::table('j_stones', function (Blueprint $table) {
        $table->dropForeign(['j_stone_types_id']);
        $table->dropUnique(['j_stone_types_id','shape','size','carat']);
    });

}

【问题讨论】:

    标签: php mysql laravel laravel-5


    【解决方案1】:

    这是reported Laravel bug 的解决方法

    public function up() {
        Schema::table('contact_tags', function (Blueprint $table) {            
            $table->unique(['tag_id', 'contact_id'], 'tag_contact_unique'); //so that contact cannot have the same tag multiple times
        });
    }
    
    public function down() {
        Schema::table('contact_tags', function (Blueprint $table) {
            //THE FOLLOWING 2 COMMANDS ARE THE WORKAROUND
            //Although this feels weird, we first need to add the missing indexes:
            $table->index('tag_id', 'tag_id_foreign');
            $table->index('contact_id', 'contact_id_foreign');
    
            //Now proceed with the main part of this "down" command to drop the unique index:
            $table->dropUnique('tag_contact_unique');
        });
    }
    

    【讨论】:

      【解决方案2】:

      我刚刚遇到了同样的问题,这对我有用:

          public function down()
      {
          Schema::table('j_stones', function (Blueprint $table) {
              $table->index('j_stone_types_id']);
              $table->dropUnique(['j_stone_types_id','shape','size','carat']);
          });
      }
      

      所以首先在外键约束所需的列上添加一个新索引,然后你可以删除复合键。

      【讨论】:

        【解决方案3】:

        我遇到了同样的问题。我解决它的方法是在迁移的down() 函数中执行以下操作:

        1. 删除外键
        2. 删除唯一复合索引
        3. 再次创建外键

        例如

        public function down()
        {
            Schema::table('j_stones', function (Blueprint $table) {
                $table->dropForeign(/* Foreign key name */);
            });
        
            Schema::table('j_stones', function (Blueprint $table) {
                $table->dropUnique(/* Array of fields for the composite index */);
            });
        
            Schema::table('j_stones', function (Blueprint $table) {
                /* Creating back the foreign key here, just like the way it was before running the migration */
            });
        }
        

        这样,每次我回滚迁移时,我的数据库和外键都会恢复到以前的状态。

        【讨论】:

          猜你喜欢
          • 2016-06-06
          • 2019-01-22
          • 2017-01-16
          • 2018-11-10
          • 2016-06-24
          • 1970-01-01
          • 2015-03-25
          • 2016-11-13
          • 2020-06-05
          相关资源
          最近更新 更多