0

Attempting to update a large dataset with ActiveRecord Import, I'm struggling to see why my insert is failing.

In my schema for the model, this line exists:

t.index ["variant_id", "stock_location_id"], name: "index_spree_stock_items_on_variant_id_and_stock_location_id", unique: true, where: "(deleted_at IS NULL)"

For the import, I'm trying to update many of these, when I run:

Spree::StockItem.import columns, values, :on_duplicate_key_update => [:count_on_hand]

I receive:

duplicate key value violates unique constraint "index_spree_stock_items_on_variant_id_and_stock_location_id"

but when I run:

Spree::StockItem.import columns, values, :on_duplicate_key_update =>{ conflict_target: [:index_spree_stock_items_on_variant_id_and_stock_location_id], columns: [:count_on_hand] }

I receive:

column "index_spree_stock_items_on_variant_id_and_stock_location_id" does not exist

Why does it say this column doesn't exist? Are indexes not considered columns available for duplicate key checks? How should I check for duplicate via an index which is a combination of two columns?

Thanks

Jake Penn
  • 1
  • 1

1 Answers1

1

Use column names instead of index name for conflict_target like this

Spree::StockItem.import(
  columns,
  values,
  on_duplicate_key_update: {
    conflict_target [:variant_id, :stock_location_id],
    columns: [:count_on_hand]
  }
)
Sikandar Tariq
  • 1,296
  • 1
  • 13
  • 29