devise icon indicating copy to clipboard operation
devise copied to clipboard

ActiveRecord::RecordNotUnique on create and update with existing email

Open sush opened this issue 15 years ago • 11 comments

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.

sush avatar Sep 30 '11 11:09 sush

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?

josevalim avatar Sep 30 '11 11:09 josevalim

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 :(

sush avatar Sep 30 '11 11:09 sush

Hrm, right. Which devise version? Which are your authentication_keys?

josevalim avatar Sep 30 '11 11:09 josevalim

Devise 1.4.7 and :username as authentication_key. I also tried to switch to :email but same problem.

sush avatar Sep 30 '11 11:09 sush

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?

josevalim avatar Sep 30 '11 12:09 josevalim

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 !

sush avatar Sep 30 '11 12:09 sush

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. :(

josevalim avatar Sep 30 '11 12:09 josevalim

My bad after investigation found that it was an issue with friendly_id which breaks validates_uniqueness_of.

Thanks again for your patience !

sush avatar Sep 30 '11 13:09 sush

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?

allenlsy avatar May 11 '15 02:05 allenlsy

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.

amitpatelx avatar Oct 14 '15 16:10 amitpatelx

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

acandael avatar Dec 14 '15 08:12 acandael