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

1 Answer 1

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]
  }
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.