Creating and Using Entity Keys (original) (raw)

Each entity is identified by a key that is unique within the application's Datastore instance, and consists of the following:

Specifying your own key name

The following example implicitly creates a key with a string identifier using the named parameter id:

You could alternatively set the key name directly:

Letting Datastore generate an ID to use for the key

This code shows how to use an auto-generated ID as the key:

Using the ancestor path in the key

The sequence of entities beginning with a root entity and proceeding from parent to child, leading to a given entity, constitute that entity's ancestor path. An entity, its parent, parent's parent, and so on recursively, are the entity's_ancestors_. The entities in Datastore form a hierarchical key space similar to the hierarchical directory structure of a file system.

The complete key identifying an entity consists of a sequence of kind-identifier pairs specifying its ancestor path and terminating with those of the entity itself. The constructor method for class Key accepts such a sequence of kinds and identifiers and returns an object representing the key for the corresponding entity.

The following example shows a blogging service that stores messages by revision. Messages are organized under accounts, and revisions are under messages.

...

In the sample, ('Account', 'sandy@example.com'), ('Message', 123), and ('Revision', '1')are all examples of kind-identifier pairs.

Notice that Message is not a model class; it is used only as a way to group revisions, not to store data.

As shown in the sample code, the entity's kind is designated by the _last_kind-name pair in the list: ndb.Key('Revision', '1').

Using named parameters

You can use the named parameter parent to designate any entity in the ancestor path directly. All of the following notations represent the same key:

Specifying a root entity

For a root entity, the ancestor path is empty and the key consist solely of the entity's own kind and identifier.

Specifying an entity with ancestors

To insert a new message with parent keys

For keys that were created with a parent, the parent() method returns a key representing the parent entity:

Using Numeric Key IDs

You can create an entity without specifying an ID, in which case the data store automatically generates a numeric ID. If you choose to specify some IDs and then let Datastore automatically generate some IDs, you could violate the requirement for unique keys. To avoid this, reserve a range of numbers to use to choose IDs or use string IDs to avoid this issue entirely.

To reserve a range of IDs, use the model class'allocate_ids()class method:

Allocating IDs

To allocate 100 IDs for a given model class MyModel:

To allocate 100 IDs for entities with parent key p:

The returned values, first and last, are the first and last IDs (inclusive) that are allocated. You can use these to construct keys as follows:

These keys are guaranteed not to have been returned previously by the data store's internal ID generator, nor will they be returned by future calls to the internal ID generator. However, the allocate_ids() method does not check whether the IDs returned are present in the data store; it only interacts with the ID generator.

To allocate all IDs up to a given maximum value:

This form ensures that all IDs less than or equal to N are considered allocated. The return values, first and last, indicate the range of IDs reserved by this operation. It is not an error to attempt to reserve IDs already allocated; if that happens, first indicates the first ID not yet allocated andlast is the last ID allocated.