本文介绍了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 排除属于多对多映射对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!