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
}
Deployment. Let's first take a quick look into how we deploy these five applications to App Engine. In App Engine, we can create a single Application per project.
App Engine supports the following scaling types, which controls how and when instances are created: Automatic (default) Basic. Manual.
Each Cloud Platform project can contain one App Engine application.
You can add multiple services in your project by creating an app. yaml file for each service. Include the service attribute in each app. yaml file to specify the name of the service.
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:
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
}
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With