【发布时间】:2018-01-28 07:28:25
【问题描述】:
嗨,我使用的是 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-user与comments的模型关系:
"relations": {
"comments": {
"type": "hasMany",
"model": "Comment",
"foreignKey": "userId"
}
}
【问题讨论】:
标签: node.js filter include models loopbackjs