Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't drop unique index

I have created unique index:

 $table->unique(['owner_id', 'promoter_id']);

and now i tray drop it

$table->dropUnique(['owner_id', 'promoter_id']);

General error: 1553 Cannot drop index 'connections_owner_id_promoter_id_unique': needed in a foreign key constraint (SQL: alter table connections drop index connections_owner_id_promoter_id_unique)

Also, i tried to drop foreign key before

$table->dropForeign('connections_promoter_id_foreign');

but still no results

like image 876
napalias Avatar asked Aug 06 '18 13:08

napalias


People also ask

Can unique index be dropped?

A primary key or unique key index cannot be explicitly dropped.

How do you remove a unique index?

In Object Explorer, right-click the table with the unique constraint, and click Design. On the Table Designer menu, click Indexes/Keys. In the Indexes/Keys dialog box, select the unique key in the Selected Primary/Unique Key and Index list. Click Delete.

How remove unique key from table in SQL?

The DROP CONSTRAINT command is used to delete a UNIQUE, PRIMARY KEY, FOREIGN KEY, or CHECK constraint.

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. The name of the table to modify.


2 Answers

From Laravel docs on Indexes, you can create the unique index with an assigned name:

enter image description here

So just to save you of debugging how laravel constructs the name to the index, you may assign it a name when adding the index e.g:

$table->unique(['owner_id', 'promoter_id'], 'owner_promoter_index');

Then when you drop it, you use the same name:

$table->dropUnique('owner_promoter_index');
like image 110
Oluwatobi Samuel Omisakin Avatar answered Sep 19 '22 01:09

Oluwatobi Samuel Omisakin


Based on this Drop muli-column unique key without dropping foreign key? i got this solution which also works:

Schema::table('connections', function ($table){
            $table->index('owner_id');
            $table->dropUnique(['owner_id', 'promoter_id']);
        });
like image 37
napalias Avatar answered Sep 21 '22 01:09

napalias