问题描述
我只是想知道如何处理急于加载的查询以检索具有多个关联的数据,让我解释一下我的模型:
type User struct {
Id uint
Name string
Username string
Image string
}
type Post struct {
Id unit
Content string
Author *User // I use user_id in author column for the posts table
Comments []Comments
}
type Comments struct {
Id unit
PostId uint
Message string
CommentBy *User // I use user_id in comment_by column for the comments table
}
这是检索到的数据的示例:
{
"Posts": [
{
"Id": 1,"Content": "test post","Author": {
"Id": 1,"Name": "author","Username": "h3ll0","Image": "author.png"
},"Comments": [
{
"Id": 1,"PostId": 1,"Message": "good article","CommentBy": {
"Id": 2,"Name": "second user","Username": "r3ader","Image": "reader.png"
}
},{
"Id": 2,"Message": "bad article","CommentBy": {
"Id": 3,"Name": "third user","Username": "thirD","Image": "third.png"
}
}
]
}
]
}
我想检索具有所有嵌套关联的发布数据,并将它们映射到Post结构。您将如何使用database / sql和pq(Postgres驱动程序)来完成此任务?
我根本不想使用任何ORM,我只使用数据库/ SQL和SQLx。
请记住,性能非常重要,因为帖子和评论表足够大。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)