查找在数组字段中不包含某些值的记录
Finding record which doesn't contain some value in array fields
我在节点上使用 sequelize + typescript(使用 postgresql 数据库),我有以下模型:
id: number,
someField: string,
arr1: number[],
arr2: number[]
并且我正在尝试查找 arr1
和 arr2
不包含特定值的所有记录。
据我所知,我在一个查询中的唯一选择是 Op.not
和 Op.contains
之间的混合,
所以我尝试了以下查询:
/// Number 1
where: {
arr1: {
[Op.not] : {[Op.contains]: [someValue]}
},
arr2: {
[Op.not] : {[Op.contains]: [soemValue]}
}
},
/// Number 2
where: {
[Op.not]: [
{arr1: {[Op.contains]: [someValue]}},
{arr2: {[Op.contains]: [someValue]}}
]
},
现在,数字 1 确实可以在 typescript 中编译,但是当尝试 运行 它时出现以下错误 returns:
{
"errorId": "db.failure",
"message": "Database error occurred",
"innerError":
{
"name": "SequelizeValidationError",
"errors":
[
{
"message": "{} is not a valid array",
"type": "Validation error",
"path": "arr1",
"value": {},
"origin": "FUNCTION",
"instance": null,
"validatorKey": "ARRAY validator",
"validatorName": null,
"validatorArgs": []
}
]
}
}
所以我尝试了第 2 个,它根本没有编译并出现以下 TS 错误:
Type '{ [Op.not]: ({ arr1: { [Op.contains]: [number]; }; } | { arr2: { [Op.contains]: [number]; }; })[]; }' is not assignable to type 'WhereOptions<any>'.
Types of property '[Op.not]' are incompatible.
Type '({ arr1: { [Op.contains]: [number]; }; } | { arr2: { [Op.contains]: [number]; }; })[]' is not assignable to type 'undefined'
所以问题是我做错了什么,或者换句话说,我如何在不查询所有记录和使用代码过滤的情况下进行查询
谢谢!
你必须使用 notIn
并且不包含也许它会起作用:
Official Docs: https://sequelize.org/master/manual/model-querying-basics.html
where: {
arr1: {
[Op.notIn]: someValueArray
},
arr2: {
[Op.notIn]: someValueArray
}
},
显然第二个选项是正确的,但不正确的是sequelize的类型,@ts-ignore修复了问题
我在节点上使用 sequelize + typescript(使用 postgresql 数据库),我有以下模型:
id: number,
someField: string,
arr1: number[],
arr2: number[]
并且我正在尝试查找 arr1
和 arr2
不包含特定值的所有记录。
据我所知,我在一个查询中的唯一选择是 Op.not
和 Op.contains
之间的混合,
所以我尝试了以下查询:
/// Number 1
where: {
arr1: {
[Op.not] : {[Op.contains]: [someValue]}
},
arr2: {
[Op.not] : {[Op.contains]: [soemValue]}
}
},
/// Number 2
where: {
[Op.not]: [
{arr1: {[Op.contains]: [someValue]}},
{arr2: {[Op.contains]: [someValue]}}
]
},
现在,数字 1 确实可以在 typescript 中编译,但是当尝试 运行 它时出现以下错误 returns:
{
"errorId": "db.failure",
"message": "Database error occurred",
"innerError":
{
"name": "SequelizeValidationError",
"errors":
[
{
"message": "{} is not a valid array",
"type": "Validation error",
"path": "arr1",
"value": {},
"origin": "FUNCTION",
"instance": null,
"validatorKey": "ARRAY validator",
"validatorName": null,
"validatorArgs": []
}
]
}
}
所以我尝试了第 2 个,它根本没有编译并出现以下 TS 错误:
Type '{ [Op.not]: ({ arr1: { [Op.contains]: [number]; }; } | { arr2: { [Op.contains]: [number]; }; })[]; }' is not assignable to type 'WhereOptions<any>'.
Types of property '[Op.not]' are incompatible.
Type '({ arr1: { [Op.contains]: [number]; }; } | { arr2: { [Op.contains]: [number]; }; })[]' is not assignable to type 'undefined'
所以问题是我做错了什么,或者换句话说,我如何在不查询所有记录和使用代码过滤的情况下进行查询
谢谢!
你必须使用 notIn
并且不包含也许它会起作用:
Official Docs: https://sequelize.org/master/manual/model-querying-basics.html
where: {
arr1: {
[Op.notIn]: someValueArray
},
arr2: {
[Op.notIn]: someValueArray
}
},
显然第二个选项是正确的,但不正确的是sequelize的类型,@ts-ignore修复了问题