我见过的所有示例(包括文档)都表明按关联进行过滤应使用以下语法
[contrived exampled]
User.findAndCountAll({
include: [
{
model: models.Task,
as: 'tasks',
where: {
status: 'complete'
},
},
],
})
但是这种样式将包括已完成的任务。如果您想按关联过滤而不包含关联记录怎么办?我不是在谈论事后使用 toJSON
。但在查询级别。
最佳答案
a) 如果您想查看至少有一项任务的用户,您可以删除(可选)WHERE 子句。 b) 如果不需要任务属性,您可以隐藏它们(如下所示)。
User.findAndCountAll({
include: [
{
model: models.Task,
required : true, // INNER JOIN....
attributes: [] // no task fields shown in result...
}
]
);
关于sequelize.js - Sequelize : Filter by association without including associated data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54774044/