简体   繁体   中英

Hierarchy in Google App Engine NDB

我试图在NDB中存储一个层次结构,我很困惑,如果我在构造新实体的键时应该只使用'parent'参数,还是应该在我的模型中包含一个额外的属性来保存父键?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM