1

I am Rails 5.1 with Postgresql 9.6.9 on Heroku free tier.

I recently was using a rake task with csv files to try to create a couple of batches or records. I then wanted to test the front end use and just verify my forms were working like they do in development.

However, when I went to add a new Record with the form I kept getting a 500 error. When I check my Heroku logs, I got this error.

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

It would list the id it was trying to use, which would be a lower id that was used when I imported records via the rake task and csv file.

I confirmed this truly was the issue by continuously attempting to use my form to create the record, which finally went through after about 20 attempts and it got to the correct next id.

I am not sure if this is an error caused by Postgres or Rails. It clearly was started when I started using the rake task and csv file. The csv file does have an attribute for ID which was being used for an easy check to either update or create a new file, but perhaps this was my cause.

Any advice on how to prevent this error would be great, as I have also uploaded other csv files for other models in even bigger batches. I would like to continue to do so, but only if I can avoid this error, so that I can add on one or two by the simple form I have when possible.

Thank you for your help.

Here is the rask task code that maybe is causing an issue:

CSV.foreach(filename, {col_sep: ";", headers: true}) do |row|
  game_hash = row.to_h
  game = Game.find_by(id: game_hash["id"])

  if(!game)
    game = Game.create(game_hash)
  else
    game.update(game_hash)
  end
end

1 Answer 1

1

If the issue is because of id in the game_hash then I would attempt to remove that as:

CSV.foreach(filename, {col_sep: ";", headers: true}) do |row|
  game_hash = row.to_h
  game = Game.find_by(id: game_hash["id"])

  #remove the id from the hash
  game_hash.delete :id

  if(!game)
    game = Game.create(game_hash)
  else
    game.update(game_hash)
  end
end

This should remove the id before the new Game is created.

Sign up to request clarification or add additional context in comments.

5 Comments

I have now tried that. I can confirm it does not work. I still have the same issue. it appears to be an ActiveRecord or Postgresql issue. I am not sure which one sets the ID or how that is done under the hood, so not sure which one or perhaps both are at fault.
Hmm, I am not sure here but is it possible that this is coming up because your keys are not symbolized? You can try doing game_hash.symbolize_keys! after line 2
You would have to find by game_hash[:id] as well.
That did it! By making the keys symbols, it cleared up my error. I feel like an idiot. You just made my night though. Thank you very much.
No problem. I am sure we have all been there :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.