给出Postgres 10 JSONB数据字段partner_json:
{
"guid": "a659883cedf44131a700a6f563f2c484",
"name": "Testing",
"referrerId": 1,
"communication": {
"email": [
{
"value": "email@address.com",
"primary": true,
"emailTypeCode": 2
}
],
"phone": [
{
"primary": true,
"phoneNumber": "+15705551234",
"phoneTypeCode": 4
}
]
},
"postalAddress": {
"as": {
"postalCode": "Z2P 0B1",
"streetName": "134 Some Street SW",
"unitNumber": "",
"provinceCode": 20,
"streetNumber": "1144"
},
"city": "Somecity",
"typeCode": 1
},
"partnerTypeCode": 4
}
我正在尝试使用电子邮件地址(partner_email ='email@address.com')上的Sequelize查询-我已经尝试过:
where: {
partner_json: {
communication: {
$contains: {
email: [{
value: partner_email
}]
}
}
}
},
按照https://github.com/sequelize/sequelize/issues/5173和
where: {
partner_json: {
communication: {
email: {
$contains: [{
value: partner_email
}]
}
}
}
},
如https://github.com/sequelize/sequelize/issues/7349
所示但是两个选项都抛出相同的错误(根据查询的路径略有不同):
Unhandled rejection Error: Invalid value { email: [ { value: 'email@address.com' } ] }
at Object.escape (F:\Binoids\SYML\GIT\syml-sequelize-sps\node_modules\sequelize\lib\sql-string.js:66:11)
at Object.escape (F:\Binoids\SYML\GIT\syml-sequelize-sps\node_modules\sequelize\lib\dialects\abstract\query-generator.js:934:22)
at Object._whereParseSingleValueObject (F:\Binoids\SYML\GIT\syml-sequelize-sps\node_modules\sequelize\lib\dialects\abstract\query-generator.js:2429:41)
at Object.whereItemQuery (F:\Binoids\SYML\GIT\syml-sequelize-sps\node_modules\sequelize\lib\dialects\abstract\query-generator.js:2131:21)
at Utils.getOperators.forEach.op (F:\Binoids\SYML\GIT\syml-sequelize-sps\node_modules\sequelize\lib\dialects\abstract\query-generator.js:2265:25)
at Array.forEach (<anonymous>)
at Object._traverseJSON (F:\Binoids\SYML\GIT\syml-sequelize-sps\node_modules\sequelize\lib\dialects\abstract\query-generator.js:2263:32)
at _.forOwn (F:\Binoids\SYML\GIT\syml-sequelize-sps\node_modules\sequelize\lib\dialects\abstract\query-generator.js:2243:12)
at F:\Binoids\SYML\GIT\syml-sequelize-sps\node_modules\lodash\lodash.js:4925:15
at baseForOwn (F:\Binoids\SYML\GIT\syml-sequelize-sps\node_modules\lodash\lodash.js:3010:24)
at Function.forOwn (F:\Binoids\SYML\GIT\syml-sequelize-sps\node_modules\lodash\lodash.js:13013:24)
at Object._whereJSON (F:\Binoids\SYML\GIT\syml-sequelize-sps\node_modules\sequelize\lib\dialects\abstract\query-generator.js:2242:7)
at Object.whereItemQuery (F:\Binoids\SYML\GIT\syml-sequelize-sps\node_modules\sequelize\lib\dialects\abstract\query-generator.js:2119:19)
at Utils.getComplexKeys.forEach.prop (F:\Binoids\SYML\GIT\syml-sequelize-sps\node_modules\sequelize\lib\dialects\abstract\query-generator.js:1994:25)
at Array.forEach (<anonymous>)
at Object.whereItemsQuery (F:\Binoids\SYML\GIT\syml-sequelize-sps\node_modules\sequelize\lib\dialects\abstract\query-generator.js:1992:35)
at Object.getWhereConditions (F:\Binoids\SYML\GIT\syml-sequelize-sps\node_modules\sequelize\lib\dialects\abstract\query-generator.js:2456:19)
at Object.selectQuery (F:\Binoids\SYML\GIT\syml-sequelize-sps\node_modules\sequelize\lib\dialects\abstract\query-generator.js:1140:28)
at QueryInterface.select (F:\Binoids\SYML\GIT\syml-sequelize-sps\node_modules\sequelize\lib\query-interface.js:1105:27)
at Promise.try.then.then.then (F:\Binoids\SYML\GIT\syml-sequelize-sps\node_modules\sequelize\lib\model.js:1604:34)
at tryCatcher (F:\Binoids\SYML\GIT\syml-sequelize-sps\node_modules\bluebird\js\release\util.js:16:23)
at Promise._settlePromiseFromHandler (F:\Binoids\SYML\GIT\syml-sequelize-sps\node_modules\bluebird\js\release\promise.js:512:31)
Sequelize.Op在我的代码中之前声明为:
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
const operatorsAliases = {
$contains: Op.contains
}
这是我第一次使用Op.contains。我已经在其他地方成功使用了具有类似结构(虽然没有放入数组)的Op.eq。我确实尝试了数组中的另一个字段,以确保它与电子邮件地址无关,而是相同的问题。
Postgres查询:
SELECT * FROM partner where partner_json->'communication'->'email' @> [{"value": "email@address.com"}]'
可以正常工作并检索预期的结果。
我正在使用最新版本的Sequelize和PG。
我不确定我在做什么错,因为据报道两者都可以正常工作。有人看到我要去哪里了吗?
答案 0 :(得分:1)
在尝试确定如何使用OR时遇到了相同的错误。事实证明,问题出在我如何调用OR,而不是实际值,即使错误消息显示“ Invalid value”。这对我有用:
const config = require('./src/config');
module.exports = {
plugins: [
{
resolve: 'gatsby-plugin-manifest',
options: {
icon: './src/images/logo.png',
},
}
],
};
我将此页面用作参考: Sequelize API Reference
我相信别名已被弃用,因此也许可以使用:
where: {
[Sequelize.Op.or]: [{book: true}, {hardcover: true}]
},