环回中的多个包含模型

Multiple include model in loopback

嗨,我正在使用环回 3,我需要执行此查询:

select nbd.Comment.id as commentId, 
       nbd.Comment.content as commentConted, 
       commentCreator.id as userCommentId,
       commentCreator.username userComment,
       reply.id as replyId,
       reply.content as replyContent,
       replyCreator.id as replyUserId,
       replyCreator.username as replyUser 
from nbd.Comment 
inner join nbd.User commentCreator on (nbd.Comment.userId = 
commentCreator.id)
left join nbd.Comment reply on (nbd.Comment.commentParentId = 
reply.commentParentId)
left join nbd.User replyCreator on (reply.userId = replyCreator.id)

所以,为此,我使用了这个包含过滤器:

{
"include": {
    "relation": "user",
    "scope": {
      "fields": [
        "id",
        "username"
      ]
    },
    "relation1": "comments",
    "scope1": {
      "include": [
        "user"
      ]
    }
  }
}

但是,它不起作用...

这是上下文,评论是由用户创建的,评论也可以有同样由用户创建的回复。

这是comment模型关系:

"relations": {
    "user": {
      "type": "belongsTo",
      "model": "MyUser",
      "foreignKey": "userId"
    },
    "comment": {
      "type": "belongsTo",
      "model": "Comment",
      "foreignKey": "commentParentId"
    },
    "comments": {
      "type": "hasMany",
      "model": "Comment",
      "foreignKey": "commentParentId"
    }

这是 my-usercomments 的模型关系:

"relations": {
    "comments": {
      "type": "hasMany",
      "model": "Comment",
      "foreignKey": "userId"
    }
  }

解决方案很简单,如果有人想在同一级别使用环回进行类似查询或多次包含,这里是示例:

{
  "where": {
    "field": 1
  },
  "order": "field DESC",
  "include": [
    {
      "relation": "relation1",
      "scope": {
        "fields": [
          "field1",
          "field2",
          "field3"
        ],
        "where": {
          "field2": {
            "gt": 2
          }
        }
      }
    },
    {
      "relation": "relation2",
      "scope": {
        "fields": [
          "field1",
          "field2",
          "field3",
          "field4"
        ]
      }
    }
  ]
}

使用这些结构可以生成多个包含,包含查询的最重要方面是关系,必须明确定义关系。