如何编写迁移以撤消 Laravel 中的唯一约束?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30607670/
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 11:42:10  来源:igfitidea点击:

How to write a migrate to undo the unique constraint in Laravel?

phplaravellaravel-4laravel-5database-migration

提问by cyber8200

I would do this to make my email field uniquein the table.

我会这样做以使我的电子邮件字段unique在表格中。

$table->unique('email');

$table->unique('email');

I've tried

我试过了

public function up()
{
    Schema::table('contacts', function(Blueprint $table)
    {
        $table->dropUnique('email');
    });
}

Then, when I run php artisan migrate, I got this

然后,当我运行 php artisan migrate 时,我得到了这个

enter image description here

在此处输入图片说明

It tell me that it's not there, but I'm 100% sure that it's there.

它告诉我它不在那里,但我 100% 确定它在那里。

enter image description here

在此处输入图片说明

How do write a migration to undo that ?

如何编写迁移以撤消该操作?

回答by Ali Gajani

You have to do $table->dropUnique('users_email_unique');

你必须要做 $table->dropUnique('users_email_unique');

To drop an index you must specify the index's name. Laravel assigns a reasonable name to the indexes by default. Simply concatenate the table name, the names of the column in the index, and the index type.

要删除索引,您必须指定索引的名称。默认情况下,Laravel 会为索引分配一个合理的名称。只需连接表名、索引中列的名称和索引类型。

回答by Munna Khan

This the better way to drop unique. You can use the real column name here.

这是放弃唯一性的更好方法。您可以在此处使用真实的列名。

$table->dropUnique(['email']);