问题
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