Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop Unique Index Laravel 5

I kept getting this while run php artisan migrate

SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'email'; check that column/key exists

While I see that email is exist on my database.

enter image description here


My migration script. I was trying to drop the unique constraint.

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AlterGuestsTable3 extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('guests', function(Blueprint $table)
        {
            $table->dropUnique('email');

        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('guests', function(Blueprint $table)
        {

            $table->dropUnique('email');

        });
    }

}

Did I forget to clear any caches ?

Any hints for me ?

like image 281
code-8 Avatar asked Mar 20 '16 01:03

code-8


2 Answers

By official documentation You can see following:

If you pass an array of columns into a method that drops indexes, the conventional index name will be generated based on the table name, columns and key type:

Schema::table('geo', function ($table) {
    $table->dropIndex(['state']); // Drops index 'geo_state_index' 
});



You can drop it just simply using [] around field name:

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

UPD: By the latest docs for 9.x it's still relevant.

like image 136
num8er Avatar answered Nov 16 '22 19:11

num8er


When dropping indexes, Laravel will expect that the full name of the index be given.

You can check your database for the full name of the index, but if the key was generated by a previous Laravel migration, its name should conform to a single, simple naming convention.

Here is what the documentation has to say about its naming convention (as of v5.2):

By default, Laravel automatically assigns a reasonable name to the indexes. Simply concatenate the table name, the name of the indexed column, and the index type.

My guess is this is why you are getting an error. There is no email index, but there probably is a guests_email_unique index.

Give this migration a shot:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AlterGuestsTable3 extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('guests', function(Blueprint $table)
        {
            $table->dropUnique('guests_email_unique');

        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('guests', function(Blueprint $table)
        {
            //Put the index back when the migration is rolled back
            $table->unique('email');

        });
    }

}

I understand it is a little confusing that when creating an index you specify the column names, but when dropping the index later you need to supply the index's full name.

Please note that I've adjusted the down() method as well, so that it reverts dropping the unique index by adding it back.

like image 40
stratedge Avatar answered Nov 16 '22 19:11

stratedge