Sequelize - does not contain a string within a PostgreSQL array query(Sequelize - 在 PostgreSQL 数组查询中不包含字符串)
问题描述
我有一个名为 Staff
的 Sequelize 模型.上面有一个数组字段,描述如下:
I have a Sequelize model called Staff
. On it there's an array field described as follows:
const Staff = sequelize.define("staff", {
name: {
type: DataTypes.STRING,
},
email: {
type: DataTypes.STRING,
unique: true
},
password: {
type: DataTypes.STRING,
},
roles: {
type: DataTypes.ARRAY(DataTypes.STRING)
},
});
我正在尝试创建一个 GraphQL 端点,它为所有在其 roles
字段中没有instructor"的员工提供服务.
I'm trying to create a GraphQL end point which serves up all staff which don't have 'instructor' in their roles
field.
我已阅读 this page of the docs,建议我使用Sequelize.Op
的组合.所以我创建了这个:
I've read this page of the docs, suggesting that I use a combination of Sequelize.Op
. So I created this:
return Staff.findAll({
where: {
roles: {
[Sequelize.Op.not]: {[Sequelize.Op.contains]: ['instructor']},
}
}
}
)
上面抛出错误:
"message": "values.map is not a function"
但是,如果我要尝试不是组合的查询,例如:
However, if I am to try a query which isn't a combo such as:
return models.Staff.findAll({
where: {
roles: {
[Sequelize.Op.contains]: ['instructor']
}
}
}
)
查询运行正常,这让我相信我可能有语法错误或对 Op
逻辑如何组合的误解.
The query runs properly, this leads me to believe I perhaps have a syntax error or misunderstanding of how Op
logic is combined.
推荐答案
试试这个:
return models.Staff.findAll({
where: {
$not: {
roles: {
$contains: ['instructor'],
},
},
},
})
子句 $not
必须在您的目标列之前.
The clause $not
need to be prior than the column you target.
这篇关于Sequelize - 在 PostgreSQL 数组查询中不包含字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Sequelize - 在 PostgreSQL 数组查询中不包含字符串


- Fetch API 如何获取响应体? 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07