【问题标题】:Add multiple values to the postgresql using nodeJS Sequelize使用 nodeJS Sequelize 向 postgresql 添加多个值
【发布时间】:2020-07-07 15:19:38
【问题描述】:

有没有办法向列添加多个值? 现在我有一个 cmets 专栏,我有这个代码。

app.put('/addComment/:id', (req, res) => {

Posts.update({
        comment: req.body.comment
    },
    {
        where: {
            id: req.params.id
        }
    }).then((post) => {
    res.send(post)
}).catch((err) => {
    console.log("err", err);
});
})

但不是添加另一个值,它只是用 new 更新旧值,有什么方法可以解决我的问题吗?

【问题讨论】:

  • 多个值是什么意思?你的专栏是文字吗?您希望将列中的现有文本与新文本连接起来吗?
  • 如果你想添加另一个值为什么你使用更新方法?用于更改现有值的更新方法。
  • 列 data_type 是 text[],我知道我不应该使用更新,但我是续集新手,找不到任何其他适合我的问题的方法
  • 你能建议我解决这个问题的方法吗?

标签: node.js sequelize.js


【解决方案1】:

可以使用concat函数sequelize

它将新值附加到列中。

const {fn, col } = models.sequelize

Posts.update({
 members: fn('CONCAT', col("comment"), req.body.comment)
}, {
 where: {
  id: req.params.id
 }
}).then(function() {
 console.log("Value Appended");
});

【讨论】:

  • 导入Sequelize
猜你喜欢
  • 2022-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-08
  • 1970-01-01
  • 2014-12-13
  • 2016-08-25
  • 1970-01-01
相关资源
最近更新 更多