如何在 Go 中的 App Engine 上实现一对多?
如何使用 Go 编程语言在 Google App Engine 上实现一对多?
例如,如果我有下面的结构,我将如何存储许多投票与一条评论的关联?我会使用注释结构中投票键的数组(切片),还是投票结构中注释的一个键?
type Comment struct {
Author string
Content string
Date datastore.Time
}
type Vote struct {
User string
Score int
}
How would I implement one-to-many on Google App Engine in the Go programming language?
For example, if I have the structs below, how would I store the association of many Votes to one Comment? Would I use an array (slice) of keys to Votes in the Comment struct, or one key to the Comment from the Vote struct?
type Comment struct {
Author string
Content string
Date datastore.Time
}
type Vote struct {
User string
Score int
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当前版本的 Go AppEngine SDK 中允许的字段类型仅有 如下:
鉴于此,似乎有两种方法可以做到这一点。一种是维护一组键来指向给定评论的投票。然而,对于任何相当受欢迎的评论来说,这可能会达到 100 个元素的限制。
另一种方法是在每个投票结构中存储一个指向评论的“指针”,如下所示:
然后,当您去查询它时,您需要分两步进行。首先,您会得到您感兴趣的评论(在本例中,只是恰好返回的第一个评论)。其次,您查询“指向”该评论的所有投票:
The only types that are allowed for fields in the current version of the Go AppEngine SDK are as follows:
Given that, there appear to be two ways to do this. One is to maintain a slice of keys to point to the Votes of a given Comment. However this is likely to run up against the 100 element limit for any reasonably popular comment.
The other approach is to store a "pointer" to the comment in each vote struct like this:
Then when you go to query it you need to do it in two steps. First you get the Comment you're interested in (in this case just the first one that happens to be returned). Second, you query for all the votes that "point" to that comment:
如何使用祖先路径将投票存储为评论的子项?我的意思是在存储每个新的 Vote 结构时设置指向父 Comment 的父键参数。像这样的东西:
How about to store Votes as child items of Comment, using ancestor paths? I mean set parent key parameter pointing to parent Comment when you storing each new Vote struct. Something like this:
我还没有尝试过,但也许值得尝试:
I haven't tried this, but maybe it's worth trying: