according to Ancestor Queries from the App Engine docs, I can do something like this:
type Team struct {
Name string
}
type Player struct {
Name string
}
// Save data first just for the test case
teamA := datastore.NewIncompleteKey(c, "Team", nil)
teamA, _ = datastore.Put(c, teamA, Team{"Team A"})
playerA := datastore.NewIncompleteKey(c, "Player", teamA)
playerA, _ = datastore.Put(c, playerA, Player{"Player A"})
playerB := datastore.NewIncompleteKey(c, "Player", teamA)
playerB , _ = datastore.Put(c, playerB, Player{"Player B"})
// query data
q := datastore.NewQuery("Team").Filter("Name=", "Team A").Limit(1).KeysOnly()
teams, _ := q.GetAll(c, nil)
q = datastore.NewQuery("Player").Ancestor(teams[0])
var players []Player
q.GetAll(c, &players)
However... What if I want to have Team to include a pointer to an slice of player, so I would save it as nil, and when I query it, I would assign it, sort of like this:
type Team struct {
Name string
Players *[]Player `datastore:-`
}
type Player {
Name string
}
// Save data first just for the test case
teamA := datastore.NewIncompleteKey(c, "Team", nil)
teamA, _ = datastore.Put(c, teamA, Team{"Team A", nil})
/* Saving player data goes here */
// query data
q := datastore.NewQuery("Team").Filter("Name=", "Team A").Limit(1)
var teams []Team
teamKeys, _ := q.GetAll(c, teams)
q = datastore.NewQuery("Player").Ancestor(teamKeys[0])
q.GetAll(c, teams[0].Players)
Would that be a good approach for simulating relationships?
For my app, a tree structure would be the ring that perfectly fits my finger.
Or maybe... do you have another suggestion?
I don't think a pointer is a valid type for datastore!
From here: https://developers.google.com/appengine/docs/go/datastore/reference
An entity's contents are a mapping from case-sensitive field names to values. Valid value types are:
- 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,
- time.Time,
- appengine.BlobKey,
- []byte (up to 1 megabyte in length),
- structs whose fields are all valid value types,
- slices of any of the above.
Or maybe... do you have another suggestion?
A slice to Player may work (not tested)
Or maybe you should search for "The PropertyLoadSaver Interface" in the same URL. It just something like you choose what and how you insert thing to datasotre rather than let it auto.