Rails 4 Devise / Omniauth电子邮件存在于数据库错误中 [英] Rails 4 Devise/Omniauth Email exists in database error
问题描述
我不知道我应该做什么我在我的数据库中创建了一个用户名列,我确定将其添加到我的devise.rb intializer, config.authentication_keys = [:username]
我认为这是因为默认的Devise安装在电子邮件coulmn上创建了唯一的密钥。在你的情况下,它是空的,因此第二个用户没有唯一的用户(因为空字符串被第一个用户占用)。
我的情况下迁移
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
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 ]
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
这篇关于Rails 4 Devise / Omniauth电子邮件存在于数据库错误中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!