ActiveRecord::RecordNotUnique on create and update with existing email
Hi,
I have upgraded to rails 3.1 and I encounter an error when I try to create (or update) a user with an existing email (I want email to be unique).
I get this error with PostgreSQL :
ActiveRecord::RecordNotUnique in Devise::RegistrationsController#create
PGError: ERROR: duplicate key value violates unique constraint "index_users_on_email"
I do use :validatable in my User model (the presence and regexp validates work) and I also tried to remove it and implement all the validations in my User model but I get the same error.
It makes sense to raise an exception on create but it doesn't on update. If this is the case, it appears to be more of a Rails issue. Why is Rails creating a new record instead of updating the exist one?
But even if it makes sense to raise an exception on create I don't understand why Devise does not show me an error message in my form (as it used to do) instead of raising me a raw error :(
Hrm, right. Which devise version? Which are your authentication_keys?
Devise 1.4.7 and :username as authentication_key.
I also tried to switch to :email but same problem.
Weird, I have no idea why this is happening. Does it work for any other model (which is not Devise one)? I still doubt this is a bug in Devise. :( For example, are you using the latest PG adapter? Does it work on Rails 3.1.1.rc2?
Yes I have the last PG adapter and I get the same error with Rails 3.1.1.rc2, weird as it was working very well before.
I have also notice that the validates_uniqueness_of :username of my User model is not interpreted :(
Thanks for your patience !
I would suggest you to try to isolate the bug in a separate application to understand what is happening better. Something seems very odd with your setup. :(
My bad after investigation found that it was an issue with friendly_id which breaks validates_uniqueness_of.
Thanks again for your patience !
I encountered this problem recently. I'm using Rails 4.2.1, Devise 3.4.1
Here is the server log:
ActiveRecord::RecordNotUnique: Mysql2::Error: Duplicate entry '[email protected]' for key 'index_users_on_email': INSERT INTO `users` ...
app/controllers/api/registrations_controller.rb ? in create_wo_oauth (application)
if @user.save
I doubt that it was because a user click on register twice too quick, so when the 2nd POST request arrived at the server, the 1st request was not finished, so the 2nd passes the Rails uniqueness validation. Then the 1st request finishes, user was created. The 2nd request hits the DB, and caused this problem.
@josevalim Is this possible?
I am getting same error with Rails 4.2.3 and Devise 3.5.1 and postgresql. I am using omniauth with Devise and trying to signup using twitter.
I am also encountering this issue:
PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_users_on_email"
The odd thing is that I myself can login with CAS, but anyone else in my organisation trying to log in gets the above error.
My version of rails is 4.2.2 the version of devise is: 3.5.2
I am using the devise_cas_authenticatable gem