0

尝试使用 ActiveRecord 导入更新大型数据集时,我很难理解插入失败的原因。

在我的模型架构中,存在这一行:

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)"

对于导入,我正在尝试更新其中的许多,当我运行时:

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

我收到:

duplicate key value violates unique constraint "index_spree_stock_items_on_variant_id_and_stock_location_id"

但是当我运行时:

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] }

我收到:

column "index_spree_stock_items_on_variant_id_and_stock_location_id" does not exist

为什么说这个专栏不存在?索引是否不被视为可用于重复键检查的列?我应该如何通过由两列组合而成的索引来检查重复项?

谢谢

4

1 回答 1

1

conflict_target像这样使用列名而不是索引名

Spree::StockItem.import(
  columns,
  values,
  on_duplicate_key_update: {
    conflict_target [:variant_id, :stock_location_id],
    columns: [:count_on_hand]
  }
)
于 2019-11-25T08:37:27.470 回答