我有一个像这样的 JSON 字段。
{Price:0,Title:'Test'}
。我想使用sequelize 将 MYSQL 上的 Price 值更新为 10。
我正在使用 MYSQL 8 并支持 JSON 字段。但是在sequelize中想要更改仅价格,sequelize直接写入带有{Price:10}的字段,而其他字段正在删除。我该如何解决这个问题?
(async () => {
await e_products_v2.update(
{
Preferences: {
Price: 0,
},
},
{ where: { id: 1 } }
);
})();
let p = await db.e_products_v2.findOne({ where: { id: 20 } })
p.Preferences = { ...p.Preferences, Price: 10 }
await p.update({ Preferences: p.Preferences })
这里更新 json 列bank_details 中的branch_details,它保留了branch_details json 列中的其他详细信息。
希望这对那些使用嵌套 JSON 的人有用。
const record = await Test.findOne({ where: { id: 1 } });
record.bank_details = [{
...record.bank_details.get(),
branch_details: { name: 'Branch Name', ifsc: 'UTI00001111' }
}];
await record.save();