Reputation: 501
I want to store hierarchical entities in the datastore. The children entities would have different kind, to represent something like this :
type EntityA struct {
Id string
LeafA *EntityA
LeafB *EntityB
SomeValue string
}
type EntityB struct {
Id string
OtherValue string
}
I planned to use ancestors, but it seems impossible to retrieve the children of a common ancestor that have different kind.
To retrieve the whole parent, is it possible to query all children of a common ancestors without specifying the kind ?
Or is there another possibility to solve this problem ?
Upvotes: 2
Views: 163
Reputation: 39834
From Using ancestor paths:
The complete key identifying the entity consists of a sequence of kind-identifier pairs specifying its ancestor path and terminating with those of the entity itself:
[Person:GreatGrandpa, Person:Grandpa, Person:Dad, Person:Me]
For a root entity, the ancestor path is empty and the key consists solely of the entity's own kind and identifier:
[Person:GreatGrandpa]
In other words the key of any of the entities in an entity group (including the root entity - the common ancestor) is a list of keys for the entire ancestry path, which starts with the root entity's key.
So, to obtain the root entity key from any of the descendants just get the 1st element in that entity's key list.
Not familiar with go
, but in python
a particular item in the ancestry path is a tuple (pair) and to obtain the root entity key from any entity key it'd be something like this:
root_entity_key = ndb.Key(pairs=tuple(list(entity_key.pairs())[0])
Now, with the root entity (common ancestor) key you can perform ancestor queries for any descendant kind you desire.
Upvotes: 1