Monday, 3 April 2017

Sequelize.js: How to query N:M relation

I have the following relations in sequelize:

Location.Offers = Location.belongsToMany(JobOffer, {
  foreignKey: 'locationId',
  through: 'JobOffer_Location',
  as: 'offers',
});

JobOffer.Locations = JobOffer.belongsToMany(Location, {
  foreignKey: 'jobOfferId',
  through: 'JobOffer_Location',
  as: 'locations',
});

I'm not being able to query a job offer based on it's location tough:

const locations = [1, 2]
JobOffer.findAll({
  include: [{
    model: Location,
    as: 'locations',
    where: {
      locationId: {
        $in: locations
      }
    },
  }],
})

Error: Only HasMany associations support include.separate

I have tried almost any solution I could find online, but nothing seems to work. Ideas?



via Fábio Santos

No comments:

Post a Comment