LoginSignup
3
1

More than 3 years have passed since last update.

【laravel】 migrationでindexの削除

Posted at

削除出来ない

usersテーブルのtextカラムにindexを以下のように貼った時、

 $table->index('text');

削除を以下のようにして php artisan migrate:rollback を実行すると。

$table->dropIndex('text');

PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'text'; check that column/key exists")

怒られる。

なぜ

laravelはindex名は命名規則に基づいて定められる。

'users_id_primary' // 主キー
'users_email_unique' // ユニークキー
'users_name_index' // 基本インデックス
'users_location_spatialindex' // 空間インデックス

なので、今回の場合はindex名は users_text_index となっている。

そういうindexは無いので怒られる。

解決

消したいindexが貼ってあるカラム名を配列にして入れることで、命名規則に従ったindexが削除される。

$table->dropIndex(['text']);
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1