鍍金池/ 問答/數據庫  HTML/ sequelize連表查詢嵌套太深

sequelize連表查詢嵌套太深

多表連接查詢數據嵌套過深有沒有遇到?
以前是連四張表成功時返回的數據。
大致一張發(fā)布表,一張評價表(誰回復誰),一張用戶表,一張主題表。
有的對象其實不想嵌套的。比如

clipboard.png
通過uid查找誰發(fā)布的,user對象里邊的內容能否直接解放出去?

同理topicId查找到的主題
clipboard.png
有沒有什么方法合并,解決嵌套對象。???

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"
                }
            }
        ]
    }
回答
編輯回答
老梗
  1. 這還算深么。。如果user,topic里有重名字段怎么辦?還有就是comments這種數組期望的結果是什么?
  2. 如果1中的問題都已經想好了,那也可以,想要扁平部分用attributessequelize.literal,想要結構部分用include。
  3. 其實查詢出來再格式化數據會好很多。
2018年7月10日 20:59