Sequelize 排除属于多的映射对象 [英] Sequelize exclude belongs-to-many mapping object

查看:11
本文介绍了Sequelize 排除属于多的映射对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在对对象进行 SequelizeJS 查询时,包括一个具有多属于关联的关系,让所包含的属性不返回带有结果的关联映射对象?

Is there a way when making a SequelizeJS query on an object, and including a relation which has a belongs-to-many association, to have the included property not return the association mapping object with the result?

即:

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
        }
    ]        
}

推荐答案

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

Found a solution to this in a github comment: 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
        }
    ]
});

这篇关于Sequelize 排除属于多的映射对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆