多表连接查询数据嵌套过深有没有遇到?
以前是连四张表成功时返回的数据。
大致一张发布表,一张评价表(谁回复谁),一张用户表,一张主题表。
有的对象其实不想嵌套的。比如
通过uid查找谁发布的,user对象里边的内容能否直接解放出去?
同理topicId查找到的主题
有没有什么方法合并,解决嵌套对象。???
await infoListModel.findAll({
include:[{
model:User,
attributes:{exclude:['updatedAt','unionId','createdAt','gender']}
},
{
model:Topic,
attributes:{exclude:['updatedAt','unionId','createdAt','gender']}
},{
// 评论
model:commentModel,
attributes:{exclude:['createdAt']},
include:[{
model:User,
as:'from',
attributes:["uid","username"]
},{
model:User,
as:'to',
attributes:["uid","username"]
}],
},
// 点赞
{
model:thumbsUp,
include:{
model:User,
attributes:["uid","avatar"]
}
// attributes:{exclude:['updatedAt','unionId','createdAt','gender']}
}],
attributes:{exclude:['createdAt']},
order:[['updatedAt','DESC']]
})
{
"infoId": 1,
"uid": 1,
"topicId": 1,
"content": "1",
"like_count": 21,
"post_count": 1,
"updatedAt": "2018-06-06T07:10:33.000Z",
"user": {
"uid": 1,
"username": "陈二",
"avatar": "https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIHmiadgySExiakKxsHobq70sxZDyic1BORkNqJ5nezISMeZoh1om6ysG53dbAoYjK0FetXV83O8icUiaw/132"
},
"topic": {
"topicId": 1,
"title": "孤独",
"des": "你有朋友吗,有那种可以深夜想聊天,一个电话就能找到的朋友。没有吧"
},
"comments": [
{
"id": 2,
"infoId": 1,
"from_uid": 1,
"to_uid": 1,
"topic_id": 1,
"content": "nidayede",
"from": {
"uid": 1,
"username": "陈韦4"
},
"to": {
"uid": 1,
"username": "六六"
}
},
{
"id": 3,
"infoId": 1,
"from_uid": 1,
"to_uid": 1,
"topic_id": 1,
"content": "nidayede",
"from": {
"uid": 1,
"username": "琪琪"
},
"to": {
"uid": 1,
"username": "让她"
}
},
{
"id": 1,
"infoId": 1,
"from_uid": 1,
"to_uid": 2,
"topic_id": 1,
"content": "1",
"from": {
"uid": 1,
"username": "哈哈"
},
"to": {
"uid": 2,
"username": "陈"
}
}
],
"thumbsUps": [
{
"id": 1,
"infoId": 1,
"uid": 1,
"user": {
"uid": 1,
"avatar": "https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIHmiadgySExiakKxsHobq70sxZDyic1BORkNqJ5nezISMeZoh1om6ysG53dbAoYjK0FetXV83O8icUiaw/132"
}
},
{
"id": 2,
"infoId": 1,
"uid": 1,
"user": {
"uid": 1,
"avatar": "https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIHmiadgySExiakKxsHobq70sxZDyic1BORkNqJ5nezISMeZoh1om6ysG53dbAoYjK0FetXV83O8icUiaw/132"
}
}
]
}
可以这样解决:
首先假设模型 A,B,C:
其次以这样的格式:
最后输出结果是:
说明:
参考文档:
Associations - 关联