Reputation: 227
Laravel version 5.1.43(LTS)
I use php artisan migrate:rollback
in terminal then return error messages. But the database is changed. Then I re-entered this command again there is no error messages.
Can anybody help me fix this problem?
[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP'user_id'; check that column/key exists (SQL: alter table
crm_user
drop indexuser_id
)[PDOException] SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'user_id'; check that column/key exists
My migration code
public function down()
{
if (Schema::hasColumn('crm_user', 'user_id')) {
Schema::table('crm_user', function (Blueprint $table) {
$table->dropColumn('user_id');
$table->dropIndex('user_id');
});
}
}
Upvotes: 3
Views: 1393
Reputation: 468
$table->dropIndex(['user_id']);
To drop an index you need to specify the column's name in []
.
And also first you need to drop the index then the column.
$table->dropIndex(['user_id']);
$table->dropColumn('user_id');
Upvotes: 0
Reputation: 71
From the documentation
To drop an index, you must specify the index's name. 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.
Drop a basic index from the "geo" table.
$table->dropIndex('geo_state_index');
Upvotes: 0
Reputation: 28941
Your index is dropped automatically when you drop your column. So then when you try to drop the index separately, you get the error that it doesn't exist.
So either swap the order, and drop the index first:
$table->dropIndex('user_id');
$table->dropColumn('user_id');
Or just drop the column, and don't worry about the index.
From the MySQL manual:
If columns are dropped from a table, the columns are also removed from any index of which they are a part. If all columns that make up an index are dropped, the index is dropped as well.
Upvotes: 7