简体   繁体   English

使用不同条件更新 sequelize 中的多行

[英]Update multiple rows in sequelize with different conditions

I'm trying to perform an update command with sequelize on rows in a postgres database.我正在尝试对 postgres 数据库中的行执行带有 sequelize 的更新命令。 I need to be able to update multiple rows that have different conditions with the same value.我需要能够更新具有相同值的不同条件的多行。

For example, assume I have a user table that contains the following fields:例如,假设我有一个包含以下字段的用户表:

  • ID身份证
  • First Name名字
  • Last Name姓氏
  • Gender性别
  • Location位置
  • createdAt创建于

Assume, I have 4 records in this table, I want to update records with ID - 1 and 4 with a new location say Nigeria.假设,我在这个表中有 4 条记录,我想用一个新的位置更新 ID - 1 和 4 的记录,比如尼日利亚。

Something like this: SET field1 = 'foo' WHERE id = 1, SET field1 = 'bar' WHERE id = 2像这样: SET field1 = 'foo' WHERE id = 1, SET field1 = 'bar' WHERE id = 2

How can I achieve that with sequelize?我怎样才能通过 sequelize 实现这一目标?

You can update multiple record at a time , but same updates for all records , if you want to make different updates for different conditons then you have to run that multiple time您可以一次更新多个记录,但对所有记录进行相同的更新,如果您想对不同的条件进行不同的更新,则必须多次运行。

Example :示例:

This will update fields1 to foo , where id is 1 or 4这会将fields1更新为 foo ,其中id为 1 或 4

let ids = [1,4];
Your_model.update({ field1 : 'foo' },{ where : { id : ids }}); 

This will update field1 to foo if id is 1 , and field1 to bar if id is 4这将更新field1为foo如果id是1, field1吧,如果id为4

Your_model.update({ field1 : 'foo' },{ where : { id : 1 }});
Your_model.update({ field1 : 'bar' },{ where : { id : 4 }}); 

Hope this will clear all your doubts.希望这能解决你所有的疑惑。

You can update multiple rows following your conditions, and to do that the operators are very helpful.您可以根据您的条件更新多行,为此操作员非常有帮助。

Look here: http://docs.sequelizejs.com/manual/querying.html (operators)看这里: http : //docs.sequelizejs.com/manual/querying.html (运营商)

const { Op } = Sequelize;
DisplayMedia.update(
    {
        field: 'bar'
    },
    {
        where: {
            id: {
                [Op.in]: [1, 10, 15, ..]   // this will update all the records 
            }                           // with an id from the list
        }
    }
)

There is all kinds of operators, including the range operators, or like operator ...etc有各种各样的操作符,包括范围操作符,或者像操作符...等

Also one of the important questions when it come to update, is how to update all rows?更新时的重要问题之一是如何更新所有行?

Not including where results in an error " Missing where attribute in the options parameter passed to update ".不包括where导致错误“在传递给更新的选项参数中缺少 where 属性”。

The answer is in the code bellow: provide a where with an empty object .答案在下面的代码中:提供一个带有空对象where

await DisplayMediaSequence.update({
    default: false
}, {
    where: {}, // <-- here
    transaction
});
await DisplayMediaSequence.update({ 
    default: true     <-- after all turned to false, we now set the new default. (that to show a practical expample) -->
}, {
    where: {
        id
    },
    transaction
});

we planning to save different values for same fields in multiple row there is a possible for getting all field values are same in database.我们计划为多行中的相同字段保存不同的值,有可能使数据库中的所有字段值都相同。 using for loop使用 for 循环

const {idArray,group_id} = params;

for(const item of idArray){

    const response = await Your_model.findOne({ where:{group_id,user_id:null}, order: [['id', 'DESC']] });

    await response.update({user_id:item});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM