Home > Back-end >  Key already exists Rails Devise Session Create
Key already exists Rails Devise Session Create

Time:01-24

I am using Devise in my Rails application and have overwritten the Devise Session Controller so I can update an attribute on my user model after the signin has succeeded. When, in the respond_with method, trying to do resource.update_attribute(:token_issued_at, Time.now.to_i) results in the following error:

PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_users_on_email" DETAIL: Key (email)=([email protected]) already exists.

I have tried setting the resource (user in this case) attribute token_issued_at value and then saving, however at that point it tells me the email is already taken.

resource.token_issued_at = Time.now.to_i
resource.save!

After turning validation off by calling resource.save(validate: false) it resorts back to the key violation error. Any help in solving this would be greatly appreciated!

class Users::SessionsController < Devise::SessionsController
  def create
    self.resource = warden.authenticate!(auth_options)
    sign_in(resource_name, resource)
    yield resource if block_given?
    respond_with resource
  end

  private

  def respond_with(resource, _opts = {})
    resource.update_attribute(:token_issued_at, Time.now.to_i)
    tokens = Jwt::Issuer.call(resource)
    response.headers['authorization'] = "Bearer #{tokens[0]}"
    render json: { refresh_token: tokens[1].crypted_token }, status: :ok
  end
end

CodePudding user response:

Somewhere along the line, my local databases got messed up. I was able to re-create this situation and was able to fix it by either of these two options.

Run this command in a rails console.

ActiveRecord::Base.connection.tables.each do |table_name| 
  ActiveRecord::Base.connection.reset_pk_sequence!(table_name)
end

OR, if your local data is newer and or not important.

rails db:drop db:create
  • Related