查询连接表,但不会在Sequelize中获取两个关联

考虑以下模型:

var User = sequelize.define('User', { _id:{ type: Datatypes.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true }, name: Datatypes.STRING, email:{ type: Datatypes.STRING, unique: { msg: 'Email Taken' }, validate: { isEmail: true } } }); var Location= sequelize.define('Location', { _id:{ type: Datatypes.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true }, name: Datatypes.STRING, address: type: Datatypes.STRING }); Location.belongsToMany(User, {through: 'UserLocation'}); User.belongsToMany(Location, {through: 'UserLocation'}); 

有没有办法查询UserLocation表的特定UserId并获得相应的Locations 。 就像是:

SELECT * FROM Locations AS l INNER JOIN UserLocation AS ul ON ul.LocationId = l._id WHERE ul.UserId = 8

从我能find的东西你可以做类似于:

 Location.findAll({ include: [{ model: User, where: { _id: req.user._id } }] }).then( loc => { console.log(loc); }); 

但是,当我不需要任何用户信息时,它将返回它joinUser表的LocationsUser Locations连接点和User ,而我只需要该用户的Locations 。 我所做的是工作,但是,对结点表的查询是首选,而不是在User表上查找。

我希望这是明确的。 提前致谢。

编辑

我实际上最终以不同的方式实现了这一点。 不过,我仍然会把这个问题留作一个问题,因为这应该是可能的。

将联结表声明为单独的类,就像这样

 var UserLocation = sequelize.define('UserLocation', { //you can define additional junction props here }); User.belongsToMany(Location, {through: 'UserLocation', foreignKey: 'user_id'}); Location.belongsToMany(User, {through: 'UserLocation', foreignKey: 'location_id'}); 

那么你可以查询结点表相同的任何其他模型。