Postgresql Sequelize Aggregate count函数使用where子句返回错误的值

Postgresql Sequelize Aggregate count函数使用where子句返回错误的值,postgresql,sequelize.js,Postgresql,Sequelize.js,我在postgresql中有两个表-系统和条件。 所有条件都将有一个systemID。 所有条件都将有一个字段“publishedStatus” 我的任务是获取具有关联“已发布”条件计数的所有系统 const allSystem = await db.System.findAll({ subQuery: false, attributes: ["id", "title", [db.sequelize.fn(&

我在postgresql中有两个表-系统和条件。 所有条件都将有一个systemID。 所有条件都将有一个字段“publishedStatus”

我的任务是获取具有关联“已发布”条件计数的所有系统

const allSystem = await db.System.findAll({
            subQuery: false,
            attributes: ["id", "title", [db.sequelize.fn("COUNT", db.sequelize.col("conditions.systemID")), "conditionsCount"]],
            include: [{
                model: db.Condition,
                as: "conditions",
                attributes: [],
                where: { publishedStatus: "published" },
                required: false  // <-------------
            }],
            group: ['System.id', 'conditions.systemID'],
        });
下面是我的问题

const allSystem = await db.System.findAll({
                subQuery: false,
                attributes: ["id", "title", [db.sequelize.fn("COUNT", db.sequelize.col("conditions.systemID")), "conditionsCount"]],
                include: [{
                    model: db.Condition,
                    as: "conditions",
                    attributes: [],
                    where: { publishedStatus: "published" },
                }],
                group: ['System.id', 'conditions.systemID'],
            });
当我在'include'->中使用'where'子句时,将忽略具有0'published'条件的系统

然而,如果我删除'where'子句->则包括具有0个条件的系统。但它包括所有已发布状态的条件

结果供参考:

Background:
System 1 has 10 conditions. 8 published and 2 unpublished
System 2 has 4 conditions. 0 published and 4 unpublished
System 3 has 0 conditions. 0 published and 0 unpublished

Expected Result:
System 1 = 8 conditions
System 2 = 0 conditions
System 3 = 0 conditions

Without Where Clause:
System 1 = 10 conditions
System 2 =  4 conditions
System 3 =  0 conditions

With Where Clause: (Only 1 row is returned)
System 1 = 8 conditions

我如何达到我想要的结果?请提供帮助。

您需要
required:false
才能使外部联接包含空条件

const allSystem = await db.System.findAll({
            subQuery: false,
            attributes: ["id", "title", [db.sequelize.fn("COUNT", db.sequelize.col("conditions.systemID")), "conditionsCount"]],
            include: [{
                model: db.Condition,
                as: "conditions",
                attributes: [],
                where: { publishedStatus: "published" },
                required: false  // <-------------
            }],
            group: ['System.id', 'conditions.systemID'],
        });
const allSystem=wait db.System.findAll({
子查询:false,
属性:[“id”、“title”、[db.sequelize.fn(“COUNT”、db.sequelize.col(“conditions.systemID”)、“conditionScont”],
包括:[{
型号:db.Condition,
作为:“条件”,
属性:[],
其中:{publishedStatus:“published”},
必填项:false//