I'm using ActiveRecord with Ruby (no Rails) to interface with a SQLite3 database. I fetch some data from a web API and store it in the database using find_or_create_by(some_hash). This works fine as long as the some_hash data is unique. If the some_hash already exists in the database, then the find_or_create_by method fails and gives the following sql error: SQLite3::ConstraintException: PRIMARY KEY must be unique.
I expected the find_or_create method first checks my database to see if the data already exists and if it does, then it will not create a duplicate entry. However if it doesn't find the data, then and only then it will create a new entry. So why do I get that error when some_hash already exists in the database like it was trying to save a duplicate entry?
How can I work around this issue?