简体   繁体   中英

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

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

$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

在此输入图像描述

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

在此输入图像描述

How do write a migration to undo that ?

You have to do $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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM