如何解决当渴望在Sequelize中加载模型时,我还获得了联结表作为我真正不需要的属性我可以避免吗?
每当我渴望加载服务器的所有用户时,每个用户都具有如下属性:
"server_user": {
"id": 5,"createdAt": "2020-08-20T00:00:00.000Z","updatedAt": "2020-08-20T00:00:00.000Z","userId": 3,"serverId": 1
}
我正在阅读文档,发现可以通过以下操作删除该字段:
If you don't want anything from the junction table,you can explicitly provide an empty array to the attributes option,and in this case nothing will be fetched and the extra property will not even be created:
Foo.findOne({
include: {
model: Bar,attributes: []
}
});
有趣的是,当我执行相同的操作时,我不再拥有所有用户的用户字段,这是有意义的,因为通过设置attributes: []
,您不会获得任何属性。这就是为什么我想知道为什么它在文档中能正常工作。
let user = await User.findByPk(userId,{
include: [
{
model: Server,include: [
{
model: User,attributes: []
}
]
}
]
})
返回此:
{
"id": 1,"userId": 1,"name": "LoL","path": "http://localhost:3000/images/servers/LoL/","thumbnail": "7d20bc13-d170-4a06-925c-9a02de6c7fa0.jpeg","endpoint": "/981368bc-ed18-42a4-a731-1d6e55593eea","createdAt": "2020-08-12T21:10:46.000Z","updatedAt": "2020-08-12T21:10:46.000Z","users": [
{
"id": 3,"username": "admin","email": "admin@admin.bg","image": "https://media.discordapp.net/attachments/733747075455647898/738360317209214976/1.png","createdAt": "2020-08-20T19:53:27.000Z","updatedAt": "2020-08-20T19:53:27.000Z","server_user": {
"id": 5,"serverId": 1
}
}
]
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。