laravel 如何删除多列上的唯一键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23902141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:33:57  来源:igfitidea点击:

How to delete Unique key on multiple columns?

laravelmigration

提问by Favourite Onwuemene

How do I reverse a unique key added on multiple columns in Laravel? Basically, what should go in the down() function of this migration code:

如何反转在​​ Laravel 中的多列上添加的唯一键?基本上,这个迁移代码的 down() 函数应该包含什么:

public function up()
{
    Schema::table('topics', function($table)
    {
        $table->unique(array('subject_id', 'grade_id', 'semester_id', 'name'));
    }
}


/**
 * Reverse the migrations.
 *
 * @return void
 */

public function down()
{
    Schema::table('topics', function($table)
    {

    }
}

回答by Marwelln

To drop a unique index, you use dropUnique('name_of_index').

删除唯一索引,请使用dropUnique('name_of_index').

If you're not specifying an index name in the second parameter of unique(), the name of the index will be tableName_fieldsSeperatedByUnderscore_unique.

如果您没有在 的第二个参数中指定索引名称,则索引名称unique()将为tableName_fieldsSeperatedByUnderscore_unique

public function down()
{
    Schema::table('topics', function($table)
    {
        $table->dropUnique('topics_subject_id_grade_id_semester_id_name_unique');
    }
}

回答by VIjay J

There are two approaches to drop unique index :

有两种方法可以删除唯一索引:

First Approach : In dropUnique() function we can pass array so that you don't need to use exact unique index name like "tableName_fieldsSeperatedByUnderscore_unique". Here is code snippet

第一种方法:在 dropUnique() 函数中,我们可以传递数组,这样您就不需要使用精确的唯一索引名称,例如“tableName_fieldsSeperatedByUnderscore_unique”。这是代码片段

Schema::table('users', function (Blueprint $table) {
      $table->dropUnique(['email']);
});

This will drop the unique index of column 'email'.

这将删除列“电子邮件”的唯一索引。

Second Approach: This approach is exactly same as described by Marwelln,still I would like to put it here again. You can pass unique index name in the dropUnique(), it will also work. But be sure that you are confident about unique index name.Code for that looks like this:

第二种方法:这种方法与Marwelln描述的完全相同,我仍然想再次放在这里。您可以在 dropUnique() 中传递唯一索引名称,它也可以工作。但请确保您对唯一索引名称充满信心。代码如下所示:

Schema::table('users', function (Blueprint $table) {
          $table->dropUnique('users_email_unique');
});