【问题标题】:Sequelize: Find Records Based on Associated BelongsToMany Record-- But Still Return All Associated Records?Sequelize:根据 Associated BelongsToMany 记录查找记录 - 但仍返回所有关联记录?
【发布时间】:2016-12-26 07:37:46
【问题描述】:

我有两个 belongsToMany 模型:

const apptsModel = db.define('Appts', {
    id: {type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true},
    [.....]
});

const UserDataModel = db.define('UserData', {
    id: {type: Sequelize.STRING, primaryKey: true},
    gender: {type: Sequelize.STRING},
    name_title: {type: Sequelize.STRING},
    name_first: {type: Sequelize.STRING},
    name_last: {type: Sequelize.STRING},
    [.....]
});

apptsModel.belongsToMany(UserDataModel, {through: 'ApptsToUsers'});
UserDataModel.belongsToMany(apptsModel, {through: 'ApptsToUsers'});

我想做一个搜索:

1) 查找至少有一个关联用户具有特定用户 ID 的所有约会。

2) 返回该约会的所有关联用户。

我已经完成了 (1) 的工作续集代码:

var ret = connectors.Appts.findAll({
    include: [connectors.UserData],
    where: {'$UserData.id$': args.originatingUserID}
}).then((res) => res.map((item) => item.dataValues));

...但它只返回一个指定用户的关联用户数据。如何返回每个约会的所有关联用户的数据?

【问题讨论】:

    标签: postgresql sequelize.js


    【解决方案1】:

    似乎还没有很多关于如何执行此操作的文档。这行得通,所以我将在此处发布以供参考。

        getAllApptsForCurrentUser(_, args) {
           return Promise.resolve()
                .then(() => {
                    //find all appointments and find those for which at least one 
                    //participating user is the one specified in originatingUserID
                    var appts = connectors.Appts.findAll({
                        include: [{
                            model: connectors.UserData,
                            where: {id: args.originatingUserID}
                        }],
                    }).then((res) => res.map((item) => item.dataValues));
                    return appts;
                })
                .then(appts => {
                    //iterate returned appointments and perform a subquery on each,
                    //finding the other participating users
                    var arrayOfPromises = [];
                    appts.forEach(function (appt) {
                        arrayOfPromises.push(
                            connectors.Appts.findOne({where: {id: appt.id}, order: [['apptDateTime']], include: [ connectors.UserData ] })
                        );
                    });
                    //Promise.all returns true when all promises passed to it have 
                    //returned true, or when one of them returns false
                    return Promise.all(arrayOfPromises);
                })
               .then(apptsWithJoinedData => {
                   //console.log(apptsWithJoinedData);
                   return apptsWithJoinedData;
               })
               .catch((err)=> {
                    console.log(err);
                });
        }
    

    如果有更好的方法,请告诉我。

    【讨论】:

    • 我也很想知道是否有更简单的方法可以在单个查询中而不是多个查询中执行此操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多