Each entity in Datastore has a key that uniquely identifies it. The key consists of the following components:
- The namespace of the entity, which allows for multitenancy
- The kind of the entity, which categorizes it for the purpose of Datastore queries
- An optional ancestor path locating the entity within the Datastore hierarchy.
An identifier for the individual entity, which can be either
- a key name string
- an integer numeric ID
Because the identifier is part of the entity's key, the identifier is associated permanently with the entity and cannot be changed. You assign the identifier in either of two ways:
- Specify your own key name string for the entity.
- Let Datastore automatically assign the entity an integer numeric ID.
Specifying a key name for an entity
To assign a key name to an entity, provide a non-empty stringID
argument to
datastore.NewKey
:
To let Datastore assign a numeric ID automatically, use an empty stringID
argument:
Assigning identifiers
You can configure Datastore to generate auto IDs using two different auto id policies:
- The
default
policy generates a random sequence of unused IDs that are approximately uniformly distributed. Each ID can be up to 16 decimal digits long. - The
legacy
policy creates a sequence of non-consecutive smaller integer IDs.
If you want to display the entity IDs to the user, and/or depend upon their order, the best thing to do is use manual allocation.
Using ancestor paths
Entities in Cloud Datastore form a hierarchically structured space similar to the directory structure of a file system. When you create an entity, you can optionally designate another entity as its parent; the new entity is a child of the parent entity (note that unlike in a file system, the parent entity need not actually exist). An entity without a parent is a root entity. The association between an entity and its parent is permanent, and cannot be changed once the entity is created. Cloud Datastore will never assign the same numeric ID to two entities with the same parent, or to two root entities (those without a parent).
An entity's parent, parent's parent, and so on recursively, are its ancestors; its children, children's children, and so on, are its descendants. A root entity and all of its descendants belong to the same entity group. 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. 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]
This concept is illustrated by the following diagram:
To designate an entity's parent, use the parent
argument to
datastore.NewKey
. The value
of this argument should be the parent entity's key.. The following example
creates an entity of kind Address
and designates an Employee
entity as its
parent: