我希望Sequelize向我返回ID不在数组中的所有值。
如果$ notIn的值是一个空数组,则Sequelize不返回任何内容,但如果数组包含空值,则返回我。
这没有给我任何回报:
db.foo.findAll({
where: {
id: {
$notIn: []
}
}
});
这会返回每个值:
db.foo.findAll({
where: {
id: {
$notIn: ['']
}
}
});
如果数组为空,它怎么不返回所有值?如果它为空,则表示应返回该值不在该数组中的所有值。由于ID不包含任何值,sequelize应该返回给我所有的值,对吗?
答案 0 :(得分:3)
此问题将在Sequelize版本4中修复。但该版本尚未完全公开。至少还没有文档。
请参阅https://github.com/sequelize/sequelize/issues/4859上的GitHub问题。
可以在https://github.com/sequelize/sequelize/blob/master/changelog.md找到更改日志。它说
[FIXED] $ notIn:[]现在转换为NOT IN(NULL)#4859
也许您可以自己在当前代码中实现这些版本4更改。
答案 1 :(得分:0)
对于那些使用< 4版本,这是我用过的工作,
let idsToSkip = [...]; //generate array of ids, this may result in empty array
idsToSkip.push('-1'); // push a value that can not be the id in table. this will generate `not in ('-1')` query serving the purpose.
db.foo.findAll({
where: {
id: {
$notIn: idsToSkip
}
}
});
希望它有所帮助!!