I'm attempting to write a webapp in Go using the Google App Engine, and I have a question about modeling relationships using the datastore.
I know in Python I would be able to model a relationship using a db.referenceProperty(). What I can't seem to figure out is how to make a similar association using the Go APIs.
Has anyone had any luck with this?
You can use Key as a property in the entity: http://code.google.com/appengine/docs/go/datastore/reference.html
Something like this (I don't know Go so bear with me):
type Employee struct {
Name string
Boss *Key
}
employee := Employee{
Name: "John Doe",
Boss: key // a key to some other entity
}
Peter, you were definitely on the right track. I think I've figured this out. I haven't really tested this, but it appears to be right in the datastore viewer. What I have right now looks like this (ignoring error checking for the example):
type Boss struct {
Name, Uuid string
}
type Employee struct {
Name, Uuid string,
Boss *datastore.Key
}
boss := &Boss {
Name: "Pointy Haired Boss",
Uuid: <<some uuid>>,
}
dilbert := &Employee {
Name: "Dilbert",
Uuid: <<some uuid>>,
boss: nil,
}
datastore.Put(context, datastore.NewIncompleteKey(context, "Boss", nil), bossman)
query := datastore.NewQuery("Boss").Filter("Uuid =", bossMan)
for t := query.Run(ctx); ; {
var employee Employee
key, err := t.Next(&employee)
if err == datastore.Done {
break
}
if err != nil {
fmt.Fprintf(w, "Error %s", err)
}
dilbert.Boss = key
}
datastore.Put(context, datastore.NewIncompleteKey(context, "Employee", nil), dilbert)