如何在 Go 中的 App Engine 上实现一对多?

发布于 2024-11-09 19:10:10 字数 321 浏览 2 评论 0原文

如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灯角 2024-11-16 19:10:11

当前版本的 Go AppEngine SDK 中允许的字段类型仅有 如下

  • 有符号整数(int、int8、int16、int32 和 int64)、
  • bool、
  • string、
  • float32 和 float64、
  • 基础类型为上述预声明类型之一的任何类型、
  • *Key、
  • appengine.BlobKey、
  • []byte(长度最多 1 MB),
  • 上述任何一个的切片(长度最多 100 个元素)。

鉴于此,似乎有两种方法可以做到这一点。一种是维护一组键来指向给定评论的投票。然而,对于任何相当受欢迎的评论来说,这可能会达到 100 个元素的限制。

另一种方法是在每个投票结构中存储一个指向评论的“指针”,如下所示:

type Vote struct {
    User string
    Score int
    CommentKey *datastore.Key
}    

type Comment struct {
    Author  string
    Content string
    Date    datastore.Time
}

然后,当您去查询它时,您需要分两步进行。首先,您会得到您感兴趣的评论(在本例中,只是恰好返回的第一个评论)。其次,您查询“指向”该评论的所有投票:

q := datastore.NewQuery("Comment").Limit(1)
comments := make([]Comment, 0, 1)
var err os.Error
var keys []*datastore.Key
if keys, err = q.GetAll(c, &comments); err != nil {
    // handle the error
}

comment := comments[0]
vq := datastore.NewQuery("Vote").Filter("CommentKey=", keys[0])

votes := make([]Vote, 0, 10)
if _, err := vq.GetAll(c, &votes); err != nil {
    // handle the error
}

The only types that are allowed for fields in the current version of the Go AppEngine SDK are as follows:

  • signed integers (int, int8, int16, int32 and int64),
  • bool,
  • string,
  • float32 and float64,
  • any type whose underlying type is one of the above predeclared types,
  • *Key,
  • appengine.BlobKey,
  • []byte (up to 1 megabyte in length),
  • slices of any of the above (up to 100 elements in length).

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:

type Vote struct {
    User string
    Score int
    CommentKey *datastore.Key
}    

type Comment struct {
    Author  string
    Content string
    Date    datastore.Time
}

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:

q := datastore.NewQuery("Comment").Limit(1)
comments := make([]Comment, 0, 1)
var err os.Error
var keys []*datastore.Key
if keys, err = q.GetAll(c, &comments); err != nil {
    // handle the error
}

comment := comments[0]
vq := datastore.NewQuery("Vote").Filter("CommentKey=", keys[0])

votes := make([]Vote, 0, 10)
if _, err := vq.GetAll(c, &votes); err != nil {
    // handle the error
}
_蜘蛛 2024-11-16 19:10:11

如何使用祖先路径将投票存储为评论的子项?我的意思是在存储每个新的 Vote 结构时设置指向父 Comment 的父键参数。像这样的东西:

key, err := datastore.Put(context, datastore.NewIncompleteKey(context, model.DB_KIND_VOTE, commentKey), &vote)

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:

key, err := datastore.Put(context, datastore.NewIncompleteKey(context, model.DB_KIND_VOTE, commentKey), &vote)
此岸叶落 2024-11-16 19:10:11

我还没有尝试过,但也许值得尝试:

type Vote struct {
    User string
    Score int
}    

type Comment struct {
    Author  string
    Content string
    Date    datastore.Time
    Votes*  []Vote
}

I haven't tried this, but maybe it's worth trying:

type Vote struct {
    User string
    Score int
}    

type Comment struct {
    Author  string
    Content string
    Date    datastore.Time
    Votes*  []Vote
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文