Consider the following models:
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'});
Is there a way to query the UserLocation table for a specific UserId and get the corresponding Locations . Something like:
SELECT * FROM Locations AS l INNER JOIN UserLocation AS ul ON ul.LocationId = l._id WHERE ul.UserId = 8
From what I can find, you can do something similar to:
Location.findAll({ include: [{ model: User, where: { _id: req.user._id } }] }).then( loc => { console.log(loc); });
However, this returns the Locations , UserLocation and User connections that it attaches to the User table when I don't need any user information, and I just need Locations for that user. What I did works, but querying the navigation table is preferable to searching the User table.
Hope this is clear. Thanks in advance.
Edit
I actually finished implementing this in a different way. However, I am still going to leave this as a question, because it should be possible.