Sequelize 查询 - 根据多对多 table 和父 table 查找记录

Sequelize Query - Finding records based on many-to-many table and parent table

给定以下续集模型:

var User = db.define('user', {
  name: Sequelize.STRING
});


var Group = db.define('group', {
  name: Sequelize.STRING,
  public : { type: Sequelize.BOOLEAN, defaultValue: true }
});

Group.belongsToMany(User, { as: 'specialUsers', through: 'user_groups', foreignKey: 'group_id' });


User.belongsToMany(Group, { through: 'user_groups', foreignKey: 'user_id' });

我将如何通过群组模型找到群组,其中返回的群组应该是用户在多对多 table 中有记录的群组 - 或者 - 群组是public 组?

我试过这样的事情:

return Group.findAll({
  attributes: ['name', 'public'],
  include: [{
    model: User,
    as: 'specialUsers',
    where: {
      $or : [
        {name: 'Neill'},
        Sequelize.literal('"group"."public" = true')
      ]
    }
  }]
});
return Group.findAll({
  attributes: ['name', 'public'],
  include: [{
    model: User,
    as: 'specialUsers',
  }],
  where: {
    $or : {
      '$users.name$": 'Neill',
      public: true
    }
  }
});

如果您使用的是较新的版本,应该可以使用。请注意,我将 where 移出了 include