I am trying to use activerecord-import to import data and I ran into an issue. In case the row already exists it needs to be updated else it needs to be created. The unique rows in the table are defined by a combination of two columns where each column value might not be unique in the table. How would I go about using the on_duplicate_key_update's conflict_target since the columns are not unique columns.
Now for the details. I have a model called Order, below is a simplified version of the columns in the Order model.
id
order_number
sku_number
quantity
The unique rows in the database are identified by a combination of order_number and sku_number. The id column is auto incremented. Normally I could do something like the following if I have an unique column as an identifier. So in the example below I would use the id column as the conflict_target assuming the id column was the unique identifier.
order = Order.create(order_number: '1', sku_number: '1', quantity: 100)
order.quantity = 200
Order.import [order], on_duplicate_key_update: {conflict_target: [:id], columns: [:quantity]}
and that would update the quantity because the order already exists.
But what I need is something like
Order.import [order], on_duplicate_key_update: {conflict_target: [:order_number, :sku_number], columns: [:quantity]}
Where the order_number and the sku_number would uniquely identify the conflict row.
But that fails since Postgres is expecting an unique column for the conflict_target.
Is there some way to call on_duplicate_key_update with non unique columns i.e. order_number and sku_number?
Any help would be greatly appreciated.