G. Hamaide
G. Hamaide

Reputation: 7106

Sequelize findAll with belongsToMany association

I have two tables related in this way :

League

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
});

User

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
});

I would like to get all the leagues that have a certain User in them : I'm doing this at the moment but I get the error 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);
      });
    });
});

How could I retrieve the leagues where there is a certain user ?

Upvotes: 0

Views: 4481

Answers (1)

Adam
Adam

Reputation: 5253

You should add your where to the included model:

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);
        });
    });
});

Update:

Its' not really nice, but I thinik, there is no better solution:

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);
            });
    });
});

Upvotes: 2

Related Questions