Sequelize Query - 根据多对多表和父表查找记录

时间:2016-02-04 20:18:29

标签: many-to-many sequelize.js

鉴于以下续集模型:

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' });

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

我尝试过这样的事情:

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

1 个答案:

答案 0 :(得分:1)

.mybadge.badge-royal {
    background-color: #886aea;
    color: #fff;
}

.mybadge {
    background-color: transparent;
    color: #AAAAAA;
    z-index: 1;
    display: inline-block;
    padding: 3px 8px;
    border-radius: 10px;
    vertical-align: baseline;
    text-align: center;
    white-space: nowrap;
    font-weight: bold;
    font-size: 14px;
    line-height: 16px;
    -webkit-box-flex: 0;
    -webkit-flex: 0 ;
    -moz-box-flex: 0;
    -moz-flex: 0;
    -ms-flex: 0;
    flex: 0;
    display: block;
    min-width: 24px;
}

如果你是一个相当新的版本,应该工作。请注意,我移动了包含

的位置