Sequelize.js:如何查询 N:M 关系

Fáb*_*tos 5 javascript node.js sequelize.js

我在续集中有以下关系:

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

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

但是,我无法根据其位置查询工作机会:

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

错误:只有 HasMany 关联支持 include.separate

我已经尝试了几乎所有可以在网上找到的解决方案,但似乎没有任何效果。想法?

Fáb*_*tos 3

不知怎的,我设法做到了:

order: [
  ["createdAt", "DESC"],
],
limit: limit,
offset: parsePage(args.page || 1, limit),
include: [{
  model: Location,
    as: 'locations',
    separate: false,
    attributes: [],
    duplicating: false,
  }],
where: {
  '$locations.id$': {
    $in: args.locations
  }
},
Run Code Online (Sandbox Code Playgroud)

https://github.com/sequelize/sequelize/issues/4446

不知道发生了什么,如果有人知道我想理解它:)