I'm trying to use the upsert_all method to update and insert all records (from ActiveRecord models) at the same time in my Postgres database. However, I come across a problem.
I first fetch existing records and update them, that makes sense as the record ID (which is my primary key) is needed to update it using the upsert_all method.
For new records though, I don't have an ID, but have to set one as upsert_all requires to have the same keys for all records, so I must set the ID, and I though setting it to nil might work. When doing this, I get the following error:
PG::NotNullViolation: ERROR: null value in column "id" violates not-null constraint
So how am I supposed to use the upsert_all method in Rails?
Is there a way to specify that if the ID is nil, the value is bypassed and auto-incremented by postgres?