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.
My migration script. I was trying to drop the unique constraint.
<?php use IlluminateDatabaseSchemaBlueprint; use IlluminateDatabaseMigrationsMigration; 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 ?
Advertisement
Answer
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 IlluminateDatabaseSchemaBlueprint; use IlluminateDatabaseMigrationsMigration; 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.