【发布时间】:2021-10-24 16:45:51
【问题描述】:
我在节点上使用 sequelize + typescript(使用 postgresql db)并且我有以下模型:
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 中编译,但在尝试运行它时返回以下错误:
{
"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'
所以问题是我做错了什么,或者换句话说,如何在不查询所有记录并使用代码过滤的情况下进行查询
谢谢!
【问题讨论】:
标签: sql node.js typescript postgresql sequelize.js