使用where条件批量创建Sequlize



我想更新从对象数组中到达的多行。目前我的代码正在工作,但似乎进展缓慢。

我的当前代码:

async function updateData(array){
for(const item of array){
await myCustomUpdateOrCreate(item)
}}
async function myCustomUpdateOrCreate(item){
const foundItem = await myModel.findOne({where: {item.item_code,},});
if(!foundItem){
await myModel.create({...item})
}else{
await myModel.update({...item},
where:{
item.item_code,
item.otherProperty:someCondition 
}
}
}

这太多次了,我连接到Db并执行更改(在原始代码中使用事务并尝试捕获ofc(。

我想使用批量创建,但不幸的是,有两个主要问题阻止了我

  1. 我不能使用where条件,我不愿意创建或更新数组中的每个项,除非它通过了我的条件,如上所述
  2. updateOnDuplicate allways return me错误:
"there is no unique or exclusion constraint matching the ON CONFLICT specification"

(我的表包含唯一的,所以我不能理解那个错误(

我也遇到了同样的问题。你在用docker镜像做postgres吗?如果是这样的话,请尝试删除表并重建容器(我实际上删除了DB的所有卷(,这样唯一约束将应用于Model中的字段。

请在此处查看我的答案https://stackoverflow.com/questions/74085267/bulk-create-sequelize-option-updateonduplicate-throws-error-no-unique-matching-t/74085441#74085441[在此输入链接描述]1

最新更新