postgresql 使用不同条件更新 sequelize 中的多行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49643047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update multiple rows in sequelize with different conditions
提问by proton
I'm trying to perform an update command with sequelize on rows in a postgres database. I need to be able to update multiple rows that have different conditions with the same value.
我正在尝试对 postgres 数据库中的行执行带有 sequelize 的更新命令。我需要能够更新具有相同值的不同条件的多行。
For example, assume I have a user table that contains the following fields:
例如,假设我有一个包含以下字段的用户表:
- ID
- First Name
- Last Name
- Gender
- Location
- createdAt
- ID
- 名
- 姓
- 性别
- 地点
- 创建于
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 实现这一目标?
回答by Vivek Doshi
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 if id
is 1 和field1
bar if id
is 4
Your_model.update({ field1 : 'foo' },{ where : { id : 1 }});
Your_model.update({ field1 : 'bar' },{ where : { id : 4 }});
Hope this will clear all your doubts.
希望这能解决你所有的疑虑。
回答by Mohamed Allal
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
});