gpt4 book ai didi

node.js - 环回中的多个包含模型

转载 作者:太空宇宙 更新时间:2023-11-03 22:24:29 25 4
gpt4 key购买 nike

嗨,我正在使用环回 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"
]
}
}
]
}

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

关于node.js - 环回中的多个包含模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48484144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com