Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

enter image description here

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

enter image description here

How do write a migration to undo that ?

like image 910
code-8 Avatar asked Jun 02 '15 22:06

code-8


People also ask

How do I migrate to rollback?

The part with migrate:rollback is the actual command. It says that you want to rollback certain database migrations. The last part, --step=1 , is a parameter for the migrate:rollback command. By default, php artisan migrate:rollback will rollback all of your database migrations.

How do I drop a unique key constraint in MySQL?

The syntax for dropping a unique constraint in MySQL is: ALTER TABLE table_name DROP INDEX constraint_name; table_name.

How can I migrate to Laravel again?

IF you want to re-migrate all the database, you can simply do: php artisan migrate:refresh . IF you want to make sure your database to be clean with your latest changes, you can drop your entire database tables and do php artisan migrate again. Also, you can try php artisan migrate --seed if you have any seeder.

How do I refresh a particular migration in Laravel?

When you run artisan migrate then it will only run migrations that haven't already been applied unless you use migrate:refresh in which case it will reset and re-run all migrations.


2 Answers

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.

like image 75
Ali Gajani Avatar answered Sep 28 '22 00:09

Ali Gajani


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

$table->dropUnique(['email']); 
like image 36
Munna Khan Avatar answered Sep 28 '22 01:09

Munna Khan