Rails 4 Devise / Omniauth电子邮件存在于数据库错误中 [英] Rails 4 Devise/Omniauth Email exists in database error

查看:177
本文介绍了Rails 4 Devise / Omniauth电子邮件存在于数据库错误中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把头撞到墙上,试图弄清楚这一点。我决定允许用户通过Twitter,Google,我的网站或Facebook登录。问题是twitter不提供电子邮件,所以我试图通过用户名添加用户。问题是设计不断检查电子邮件,当我不需要它,我得到PG ::错误:错误:重复键值违反唯一约束index_users_on_email细节:密钥(电子邮件)=()已经存在。 :INSERT INTOusers(created_at,name,provider,uid,updated_at,username)VALUES($ 1,$ 2,$ 3,$ 4,$ 5,$ 6)RETURNINGid



我不知道我应该做什么我在我的数据库中创建了一个用户名列,我确定将其添加到我的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆