devise_token_auth icon indicating copy to clipboard operation
devise_token_auth copied to clipboard

Exception when updating email with existing one

Open Charrette opened this issue 8 years ago • 2 comments

When I try to update an User's email with an existing one, I get:

ActiveRecord::RecordNotUnique (PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_volunteers_on_uid_and_provider"

Why is this exception not handled? I fixed it with a before_update callback in my user model such as:

  def check_email
    user = self.class.find_by(email: self.email)
    if user.present? and user.id != self.id
      errors.add(:email, "already in use")
      return false
    end
    return true
  end

But I shouldn't need to do this right?

Charrette avatar Sep 29 '16 13:09 Charrette

I get similar problem. My app uses Sidekiq, I need trigger a job after commit:

after_commit -> { RemittanceProcessJob.perform_later self }, on: :create

This job create from remittance a lot of users. On the first row I get the bellow warning:

ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_users_on_uid_and_provider" DETAIL: Key (uid, provider)=([email protected], ...

I solve this using validation on model:

class User < ApplicationRecord
  ...
  validates_uniqueness_of :uid, scope: :provider
  ...
end
class DeviseTokenAuthCreateUsers < ActiveRecord::Migration[5.0]
  ...
  # add_index :users, [:uid, :provider], unique: true
  ...
end

I believe this is not the correct way but, for now, works.

brunowego avatar Jan 30 '17 12:01 brunowego

Is this issue still open? i've got the same error although i added:

validates :uid, uniqueness: {scope: :provider, conditions: -> { with_discarded }}

to my User model

bettysteger avatar Mar 23 '22 16:03 bettysteger