Sequelize排除了属于多对的映射对象

wea*_*e08 4 sequelize.js

有没有办法在对象上创建SequelizeJS查询,并包含具有所属关系的关系时,让包含的属性不返回关联映射对象的结果?

即:

Users.findAll({include: [{model: Role, as: 'roles'}]})

//yields objects of the following form
user: {
    username: 'test',
    roles: [
        {
           name: 'user',
           UserRoles: {userId: 1, roleId: 1} //<--I do not want this
        }
    ]        
}
Run Code Online (Sandbox Code Playgroud)

wea*_*e08 16

在github评论中找到了解决方案:https://github.com/sequelize/sequelize/issues/4074#issuecomment-153054311

要旨:

Users.findAll({
    include: [
        {
            model: Role, 
            as: 'roles',
            through: {attributes: []} //<-- this line will prevent mapping object from being added
        }
    ]
});
Run Code Online (Sandbox Code Playgroud)