Rails 4.2 ActiveAdmin 1.0.0.pre2由于重复的键值违反了唯一约束" index_admin_users_on_email而无法创建AdminUser [英] Rails 4.2 ActiveAdmin 1.0.0.pre2 Cannot create a AdminUser due to duplicate key value violates unique constraint "index_admin_users_on_email

查看:81
本文介绍了Rails 4.2 ActiveAdmin 1.0.0.pre2由于重复的键值违反了唯一约束" index_admin_users_on_email而无法创建AdminUser的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难弄清楚这个错误。昨天我发布了一个问题我的AcitveAdmin电子邮件字段始终为清空。今天我意识到问题有点大。

Hi i am having a hard time figuring out this error. Yesterday i posted a question that my AcitveAdmin email field is always empty.Today i realised the problem is a bit bigger.

当我尝试在Rails控制台中添加新用户时,会发生以下情况。

When i try to add a new user in the rails console the following happens.

>> au = AdminUser.new(email: 'sample@tom.com', password: "123456789", password_confirmation: "123456789")
=> #<AdminUser id: nil, email: "", encrypted_password: "$2a$10$Xt6EpJhtQ3d1v62KNZFUk.oi1RYe2gEBBeWVd/vApvi...",reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil>

>> au.valid?
AdminUser Exists (0.8ms)  SELECT  1 AS one FROM "admin_users" WHERE"admin_users"."email" = 'sample@tom.com' LIMIT 1
=> true

>> au.save
(0.8ms)  BEGIN
AdminUser Exists (1.1ms)  SELECT  1 AS one FROM "admin_users" WHERE "admin_users"."email" = 'sample@tom.com' LIMIT 1
SQL (3.5ms)  INSERT INTO "admin_users" ("encrypted_password","created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["encrypted_password","$2a$10$Xt6EpJhtQ3d1v62KNZFUk.oi1RYe2gEBBeWVd/vApviETIlUvpQ2q"], 
["created_at", "2015-10-21 13:57:44.081780"],["updated_at", "2015-10-21 13:57:44.081780"]]
PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_admin_users_on_email"
DETAIL:  Key (email)=() already exists.
: INSERT INTO "admin_users" ("encrypted_password", "created_at","updated_at") VALUES ($1, $2, $3) RETURNING "id"
(1.2ms)  ROLLBACK
ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_admin_users_on_email"DETAIL:  Key (email)=() already exists.

但我从未使用过此电子邮件。我最近对数据库系统进行了分区。因此,现在在生产环境中,我使用其他数据库。但是对于发展而言,一切都保持不变。 admin_users 表中的一个AdminUser仍然可以访问,我可以通过ActiveAdmin仪表板登录。

But i never used this email. I recently partitioned my database system. So now in productive environment i use a different database. But for development everything stayed the same. The one AdminUser in the admin_users table is still accessible and i can login with this one through the ActiveAdmin dashboard.

我发现了类似的这里的问题,但解决方案对我不起作用。
有人有任何建议吗?

I found a similar problem here but the solutions does not work for me. Does anyone has any suggestion? Thanks to all in advance!

更新

事实证明,这里有一个地址为空的adminuser。这就是为什么我得到了重复的电子邮件字段的原因。我销毁了那个用户。现在,我可以添加另一个adminuser。但是电子邮件字段仍未写入。

It turned out there was a adminuser with a null address. So that is why i got the dublicate email field. I destroyed that user. Now i am able to add another adminuser. But the email field is still not written.

>> au = AdminUser.new(email: 'sample@tom.com', password: "123456789", password_confirmation: "123456789")
=> #<AdminUser id: nil, email: "", encrypted_password: "$2a$10$TWpMSdAxPl4vTFg54XvZcOe5Xkc7t4mf2Or7UlbkoA8...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil>

>> au.valid?
AdminUser Exists (25.6ms)  SELECT  1 AS one FROM "admin_users" WHERE "admin_users"."email" = 'sample@tom.com' LIMIT 1
=> true

>> au.save
(0.7ms)  BEGIN
 AdminUser Exists (0.8ms)  SELECT  1 AS one FROM "admin_users" WHERE "admin_users"."email" = 'sample@tom.com' LIMIT 1
 SQL (61.7ms)  INSERT INTO "admin_users" ("encrypted_password",
  "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"   
  [["encrypted_password", 
  "$2a$10$TWpMSdAxPl4vTFg54XvZcOe5Xkc7t4mf2Or7UlbkoA8W/n3rOGGwC"], 
  ["created_at", "2015-10-21 14:28:46.750437"], ["updated_at","2015-10-2114:28:46.750437"]]
   (18.0ms)  COMMIT
 => true

我也降级到了ActiveAdmin 1.0.0.pre1,但问题仍然存在。

I also downgraded to ActiveAdmin 1.0.0.pre1 but the problem is still there.

推荐答案

上下文



ActiveAdmin使用称为 Devise ,它用于创建经过身份验证的用户模型(在AcitveAdmin中为ActiveUser)。 Devise非常好,因为它可以处理和验证所有基本身份验证字段,例如电子邮件,密码,password_confirmation等。

Context

ActiveAdmin uses something called Devise that handles creating an authenticated user model (named ActiveUser in AcitveAdmin's case). Devise is really nice because it handles adding and validating all the basic authentication fields like email, password, password_confirmation, etc.

OP有一个添加 attr_accessor:email 的问题,该问题覆盖了Devise电子邮件字段:(
这意味着每次用户创建的电子邮件始终设置为

The OP had an issue of adding an attr_accessor :email which overwrote the Devise email field :( This meant that every time a user was created the email was always set to "".

但是由于系统的复杂性,ActiveRecord验证查看正确电子邮件字段并表示很好。但是,保存后,发送了空的 电子邮件(仍然是字符串)因此不会使 not null 约束无效。因此,创建了一个具有空电子邮件的ActiveUser,然后在下次创建ActiveUser时,出现了 duplicate引发错误(因为正在插入另一个电子邮件为 的用户。

But because of the complexities of the system, the ActiveRecord validation looked at the correct email field and said it was good. However, on save, the empty "" email was sent (which is still a string so doesn't invalidated the not null constraint. So an ActiveUser with a empty email was created. Then the next time a ActiveUser was created, a duplicate error was thrown (because another user with an email of "" was being inserted.

M确保您在电子邮件字段上没有访问者(或与此相关的密码/密码确认)。如果需要做一些特别的事情,请阅读Devise指南。

Make sure you don't have an accessor on the email field (or password/password_confirmation for that matter). And read the Devise guides if you need to do something fancy.

这篇关于Rails 4.2 ActiveAdmin 1.0.0.pre2由于重复的键值违反了唯一约束&quot; index_admin_users_on_email而无法创建AdminUser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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