gpt4 book ai didi

javascript - 如何追加nodejs Sequelize json字段的模型列表

转载 作者:行者123 更新时间:2023-12-03 22:19:32 24 4
gpt4 key购买 nike

如何在nodejs Sequelize中附加JSON列表

数据库:Postgres

我有一个这样的模型字段:

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

我正在尝试以这种方式附加 JSON 的 Sequelize Array,但没有成功附加列表:
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
}
}
);

关于javascript - 如何追加nodejs Sequelize json字段的模型列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61724389/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com