【问题标题】:Sequelize findAll with belongsToMany association用 belongsToMany 关联对 findAll 进行续集
【发布时间】:2016-02-03 18:03:59
【问题描述】:

我有两个以这种方式关联的表:

联赛

sequelize.define('league', {
  name: {
    type: DataTypes.STRING,
  },
  ownerId: {
    type: DataTypes.INTEGER,
  }
}, {
    classMethods: {
    associate: function(models) {
      League.belongsToMany(models.user, {
        constraints: false,
        through: models.UserLeague,
      });
    }
  }
}, {
    freezeTableName: true
});

用户

sequelize.define('user', {
  name: {
    type: DataTypes.STRING
  },
  email: {
    type: DataTypes.STRING,
    unique: true,
    validate: {
        isEmail: true
    }
  },
  profile_picture: {
    type: DataTypes.STRING
  }
}, {
    classMethods: {
    associate: function(models) {
      User.belongsToMany(models.league, {
        constraints: false,
        through: models.UserLeague,
      });
    }
  }
}, {
  freezeTableName: true
});

我想获取所有拥有特定用户的联赛:我现在正在这样做,但我收到错误 column league.users.id does not exist

sequelize.transaction(function (t) {
    return models.user.findOne({where: {email: req.query.email}}, {transaction: t}).then(function(user) {
      return models.league.findAll({where: {'users.id': user.id}, include: [{model: models.user, as: 'users'}]}).then(function(leagues) {
        res.json(leagues);
      });
    });
});

如何检索某个用户所在的联赛?

【问题讨论】:

    标签: javascript node.js sequelize.js


    【解决方案1】:

    您应该将您的where 添加到包含的模型中:

    sequelize.transaction(function (t) {
        return models.user.findOne({where: {email: req.query.email}}, {transaction: t}).then(function (user) {
            return models.league.findAll({
                include: [{
                    model: models.user,
                    as: 'users',
                    where: {
                        id: user.id
                    },
                    required: true
                }]
            }).then(function (leagues) {
                res.json(leagues);
            });
        });
    });
    

    更新:

    它不是很好,但我认为,没有更好的解决方案:

    sequelize.transaction(function (t) {
        return models.user.findOne({where: {email: req.query.email}}, {transaction: t}).then(function (user) {
            var _leagueIds = [];
            return models.league.findAll({
                include: [{
                    model: models.user,
                    as: 'users',
                    where: {
                        id: user.id
                    },
                    required: true
                }]
            })
                .each(function (league) {
                    _leagueIds.push(league.id);
                })
                .then(function () {
                    return models.league.findAll({
                        where: {
                            id: _leagueIds,
                        },
                        include: [{
                            model: models.user,
                            as: 'users'
                        }]
                    });
                })
                .then(function (leagues) {
                    res.json(leagues);
                });
        });
    });
    

    【讨论】:

    • 好的,这部分是我想要的 :) 这给了我包含我想要的用户的联盟,但返回的联盟只有想要的用户,而不是属于联盟的所有用户。你明白我的意思吗?
    • 我很确定,您需要两个查询来实现这一点。
    • 可能,但我不知道如何。第一个可以检索联盟,但如何添加联盟中的所有用户。这就是我所缺少的。不确定我在做什么需要它,但我想知道该怎么做:)
    • 我已经用这个修改了答案。
    • 确实看起来很不错。我会尽快尝试并让您知道,但谢谢!
    猜你喜欢
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    • 2020-02-03
    • 2020-12-16
    • 2014-06-30
    • 1970-01-01
    • 2020-11-12
    • 2021-01-03
    相关资源
    最近更新 更多