如何在laravel 5.2迁移中删除包含外键的组合键

zac*_*rry 2 php mysql laravel laravel-5

我有一个表(名为j_stones),其中包含5个字段:

id(primary key)  
j_stones_type(foreign-key)  
shape  
size  
carat
Run Code Online (Sandbox Code Playgroud)

我想使每一行都是唯一的,所以我创建了一个迁移来做到这一点:

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

这完美地工作,但是当我尝试回滚时,出现以下错误:

1 [Illuminate \ Database \ QueryException]

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']);
    });
}
Run Code Online (Sandbox Code Playgroud)

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

    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'); 
}
Run Code Online (Sandbox Code Playgroud)

并且也这样:

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

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

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

请指教。

编辑:
我可以使用以下技巧回滚,但仍在寻找适当的解决方案:

    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']);
    });

}
Run Code Online (Sandbox Code Playgroud)

Rya*_*yan 5

这是此报告的Laravel错误的解决方法

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');
    });
}
Run Code Online (Sandbox Code Playgroud)