繁体   English   中英

如何 append nodejs 续集 model 列表 json 字段

[英]How to append nodejs sequelize model list of json field

如何 append 在 nodejs Sequelize 中归档的 JSON 列表

数据库:Postgres

我有一个像这样的 model 字段:

student_list:
    {
        type: Sequelize.ARRAY(Sequelize.JSON),
        allowNull: true
    },

我正在尝试 append Sequelize Array of JSON 像这样但没有成功获得 append 列表:

var data =  ({'share_by':"aaa",'list_name':"list_name"})
student.update( 
                {'student_list': Sequelize.fn('array_append', Sequelize.col('student_list'), data)},
                {'where': {studet_id: student_id}}
                );

这个怎么做?

您可以执行find()并获取元素值,更新元素并使用更新的数据进行更新。 像这样:

// Get the actual student data
const student = await student.findOne(studentId);

// Spread and add the new value
const student_list = {
   ...student_list,
   data
};

// Update the field
await student.update(
    {
        student_list
    },
    {
        where: {
            student_id
        }
    }
);

暂无
暂无

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

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