postgresql - Sequelize : Querying if ARRAY contains a value

标签 postgresql sequelize.js

假设我有一个 PG ARRAY 字段:

id |    array    |
===|=============|
  1|{"1","2","3"}|

如何使用sequelize查询数组字段是否为值1

我试过:

array: { $contains: "1" }

这给了我:

array @> "1"

有错误:

Possibly unhandled SequelizeDatabaseError: array value must start with "{" or dimension information

更新

我能够通过以下方式做到这一点: array: { $contains: '{' + value + '}' }

有没有更正确的方法?

最佳答案

我意识到 sequelize 期望条件是一个数组:

array: { [Op.contains]: ["1"] }

那行得通。干杯!

请记住,Op 是从 sequelize 导出的

const { Op } = require('sequelize');

import { Op } from 'sequelize';

参见官方文档:https://sequelize.org/master/manual/model-querying-basics.html#operators

关于postgresql - Sequelize : Querying if ARRAY contains a value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29036363/

相关文章:

postgresql - NSIS 读取注册表项为 postgresql 返回空

ruby - rake 中止! PG::ConnectionBad: fe_sendauth: 没有提供密码 sinatra

python-3.x - 从 python psycopg2 客户端连接到 Azure PostgreSQL 服务器

mysql - 使用子查询和 case When 语句对查询进行序列化

sql - PostgreSQL - 数据错误 : invalid input syntax for type numeric: "Product"

postgresql - 进行 PostgreSQL 备份的最佳方法

mysql - 在 node.js 上使用 coalesce 和 sequelize

postgresql - 连接 Vue.js 前端 -> 通过 Express、Node.js、Sequelize (API) 端点。 (PostgreSQL)

javascript - 使用 Sequelize 为多对多关系创建新实例

mysql - Sequelize : only the last few rows are updated