Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
540 views
in Technique[技术] by (71.8m points)

sequelize连表查询嵌套太深

多表连接查询数据嵌套过深有没有遇到?
以前是连四张表成功时返回的数据。
大致一张发布表,一张评价表(谁回复谁),一张用户表,一张主题表。
有的对象其实不想嵌套的。比如

clipboard.png
通过uid查找谁发布的,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"
                }
            }
        ]
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

可以这样解决:

首先假设模型 A,B,C:

AModel:{ id: 'a', name: 'a_name', b_id: 'b' }
BModel: { id: 'b', name: 'b_name', c_id: 'c' }
CModel: { id: 'c', name: 'c_name' }

其次以这样的格式:

const data = await AModel.findOne({
      where,
      raw: true,
      attributes: {
        include: [
          [sequelize.col('B.id'), 'b_id'],
          [sequelize.col('B.name'), 'b_name'],
          [sequelize.col('B.C.id'), 'c_id'],
          [sequelize.col('B.C.name'), 'c_name']
        ]
      },
      include: [{
        model: BModel,
        attributes: [],
        include: [{
          model: CModel,
          attributes: []
        }]
      }]
    })

最后输出结果是:

{
    "id": "a",
    "name": "a_name",
    "b_id": "b",
    "b_name": "b_name",
    "c_id": "c",
    "c_name": "c_name"
}

说明:

  1. A,B,C 已用关系连接,比如 hasOne、belongsTo 等等,这样可以使用 include;
  2. 在 AModel 的 attributes 内的 include,是数组,是输出 A 结果之外新增的属性,这样可以避免把 A 结果属性都写一遍的麻烦;
  3. 注意 raw: true,sequelize.col 内包含的就是其形式下的属性名(最后一个字段名同数据库字段名称,比如使用的 userId,但是数据库存储的 user_id,这里就要写 user_id)

参考文档:
Associations - 关联


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...