我有一个 posgresql 数据库,表中有一列是数组:Sequelize.ARRAY(Sequelize.BIGINT)
。
将新项 append 到数组的正确方法是什么?
我是 posgresql、sequelize 和 nodejs 的新手。可能这是一个微不足道的问题。 通过阅读,我想我知道如何使用 Promise.all 来读取所有行、追加和更新回来。 问题是,有没有什么有用的捷径。
PostreSQL documentation提到一个函数 array_append(anyarray, anyelement)
。
Sequelize documentation offers一个函数 fn,它创建一个表示数据库函数的对象
,但似乎只在where and order parts
有什么方法可以将它们组合起来进行类似 append 的更新吗?
最佳答案
Array Datatype 有很多方法,比如append、concat 等,你可以看here .我将给出一个带有 array_append
函数的例子。
Room
是一个 sequelize 模型,job_ids
是该模型中的一个列,数据类型为整数数组。 sequelize
是 Sequelize 类的实例。
Room.update(
{'job_ids': sequelize.fn('array_append', sequelize.col('job_ids'), new_jobId)},
{'where': {'id': roomId}}
);
假设此列的默认值为空数组,否则可能会抛出错误。
关于arrays - 如何使用 Sequelize + PostgreSQL 将项目 append 到数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43962899/