0xcurb
0xcurb

Reputation: 126

Hierarchy in Google App Engine NDB

I'm trying to store a hierarchy in NDB and I'm confused about if I should use just the 'parent' parameter while constructing the keys to new entities or should I include an extra property in my models to hold the parent key?

Upvotes: 3

Views: 1065

Answers (2)

Tim Hoffman
Tim Hoffman

Reputation: 12986

If you use the ancestor in the key you will create a big entity group (assuming a single root to the tree/hierarchy) which may in fact not be what you want from a write performance point of view. Also a deep hierarchy can mean very big keys.

If you want to move nodes around using ancestor keys, you have to delete and recreate the entire child hierarchy of keys, where as storing the parent in the node (or the children keys in the parent) means you just store different keys in properties.

If you normally walk down the hierarchy (say url traversal) you may find it more efficient to just store the childrens keys in a list in the parent, assuming each level is not going to have too many immediate children, as well as storing the parent key in the child.

I would examine your actual requirements in detail before deciding which way to go.

Upvotes: 5

Paul Collingwood
Paul Collingwood

Reputation: 9116

The former. You don't need to create an extra property as it's already stored for you in the ancestor chain that represents the path (the key really) to the model.

Read this link: https://developers.google.com/appengine/docs/python/datastore/entities#Python_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]

To designate an entity's parent, use the parent argument to the model class constructor when creating the child entity. The value of this argument can be the parent entity itself or its key; you can get the key by calling the parent entity's key() method.

Upvotes: 1

Related Questions