Sequelize.js 用于查询具有关联的关联的单个Sequelize查询

Sequelize.js 用于查询具有关联的关联的单个Sequelize查询,sequelize.js,Sequelize.js,我有3个模型:报告、分支、客户 Report.belongsTo(Branch); Branch.belongsTo(Client); 查询报表及其相关分支和客户端时,我会运行2个查询: Report.findOne({ where:{id:reportId}, include: [{ model: Branch, attributes: ['name','clientId'] }] })

我有3个模型:报告、分支、客户

Report.belongsTo(Branch);
Branch.belongsTo(Client);
查询报表及其相关分支和客户端时,我会运行2个查询:

Report.findOne({
        where:{id:reportId},
        include: [{
            model: Branch,
            attributes: ['name','clientId']
        }]
    })
        .then((report)=>{
            const clientId = report.dataValues.branch.clientId;
            Client.findOne({
                where:{id:clientId},
            })
                .then((client)=>{
                    console.log('client is ', client);
                });
        })
        .catch();

我可以只使用一个连接报表->分支->客户端的查询来查询数据吗?

是,包含可以相互嵌套。假设为协会分支机构。belongsToClient;如果设置正确,您应该能够执行以下操作:

Report.findOne({
    where: {
        id: reportId
    },
    include: [
        {
            model: Branch,
            attributes: [ 'name', 'clientId' ],
            include: [
                {
                    model: Client
                }
            ]
        }
    ]
});