在对象上进行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
}
]
}
答案 0 :(得分:13)
在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
}
]
});
答案 1 :(得分:0)
您可以尝试指定attributes
属性。
所以要包括字段说a,b,c你添加
attributes: ['a', 'b','c']
排除他们
attributes:{exclude:['a', 'b','c']}
所以findAll看起来像
Model.someModel.findAll({
where: // some condition
include: // some other model
attributes: // some fields
})
您还可以在include子句
中指定属性