Rails 4 Devise/Omniauth Email exists in database error

依然范特西╮ 提交于 2019-12-21 21:50:18

问题


I am "banging" my head against a wall trying to figure this out. I decided to allow users to log in through twitter, Google,my site or Facebook. The problem is that twitter does not provide emails, so I am trying to add users by username. The problem with that is devise keeps checking for email and when I don't require it, I get PG::Error: ERROR: duplicate key value violates unique constraint "index_users_on_email" DETAIL: Key (email)=() already exists. : INSERT INTO "users" ("created_at", "name", "provider", "uid", "updated_at", "username") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id".

I don't know what I should do. I created a username column in my database, and I made sure to add this to my devise.rb intializer, config.authentication_keys = [ :username ]


回答1:


I think that's because default Devise installation creates unique key on email coulmn. In your case it's empty, hence second user doesn't have unique one (because empty string is taken by the first user).

Migration in my case

class AllowNullEmail < ActiveRecord::Migration
  def up
    remove_index :users, :email
    change_column :users, :email, :string, :null => true
  end

  def down
    change_column :users, :email, :string, :null => false
    add_index :users, :email, unique: true
  end
end


来源:https://stackoverflow.com/questions/17118039/rails-4-devise-omniauth-email-exists-in-database-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!