Reputation: 14581
I am using Postgres, NodeJS and Knex.
I have the following situation:
In NodeJS I have an array of objects and I need to:
a. Insert a new row, if the table does not contain the unique id, or
b. Update the remaining fields, if the table does contain the unique id.
From my knowledge I have three options:
Looking through the options I am aware of, upsert seems the most reasonable one but does it have any drawbacks?
Upvotes: 1
Views: 1236
Reputation: 5225
Upsert
is a common way.
Another way is use separate insert/update operations and most likely it will be faster:
Define existing rows
select id from t where id in (object-ids)
(*)
Update existing row by (*) result
Filter array by (*) and bulk insert new rows.
See more details for same question here
Upvotes: 2