0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

なぜかActiveRecord::RecordNotUniqueで怒られる

Last updated at Posted at 2019-06-25

状況

rails初心者です。
railsでdeviseを使ってログイン機能を作った際に新規登録のUIを変更し実行した際に
ActiveRecord::RecordNotUniqueで怒られた。

原因

主な原因はテーブルのindexにありそうでした。

db/schema.rb
# ~~省略
t.index ["username"], name: "index_users_on_username", unique: true
t.index ["nil"], :name "index_users_on_name", unique: true

このようにnilに対してのインデックスがなぜに出来たのかもどのような効果があるのかもはっきりとは分かっていませんが用意されていました。

解決策

直前に'username'というカラムを追加していました。
そこで一旦

$ rails db:rollback

テーブルを1つ前の状態に戻し

$ rails db:migrate

migrateし直した結果見事にnilに対してのindexは削除されていました!

終わりに

nilに対してのindexをremove_indexで消す方法もありなのかなと思いましたがあんまりやり方が分からず今回のやり方でうまくいったのでよしとしました。
どうしてnilに対してのindexが作られていたのかあんまり分かっていないのでどなたか詳しい方教えていただけたら嬉しいです。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?