Home:ALL Converter>Sequelize JSON field update only one field

Sequelize JSON field update only one field

Ask Time:2021-03-06T22:07:28         Author:Mahir Altınkaya

Json Formatter

I have a JSON field like this. {Price:0,Title:'Test'}. I want to update Price value with 10 on MYSQL with sequelize. I am using MYSQL 8 and JSON field supported. But in sequelize want to change Only Price with sequelize directly write to the field with {Price:10} and another fields are deleting. How can I solve this?

(async () => {
  await e_products_v2.update(
    {
      Preferences: {
        Price: 0,
      },
    },
    { where: { id: 1 } }
  );
})();

Author:Mahir Altınkaya,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/66506543/sequelize-json-field-update-only-one-field
Wang Liang :

let p = await db.e_products_v2.findOne({ where: { id: 20 } })\n \np.Preferences = { ...p.Preferences, Price: 10 }\nawait p.update({ Preferences: p.Preferences })\n",
2021-03-06T18:45:11
yy