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?