Go 1.11 has reached end of support
and will be deprecated
on January 31, 2026. After deprecation, you won't be able to deploy Go 1.11
applications, even if your organization previously used an organization policy to
re-enable deployments of legacy runtimes. Your existing Go
1.11 applications will continue to run and receive traffic after their
deprecation date. We
recommend that you migrate to the latest supported version of Go.
Stay organized with collections
Save and categorize content based on your preferences.
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:
// Create a key with a key name "asalieri".key:=datastore.NewKey(ctx,// context.Context"Employee",// Kind"asalieri",// String ID; empty means no string ID0,// Integer ID; if 0, generate automatically. Ignored if string ID specified.nil,// Parent Key; nil means no parent)
To let Datastore assign a numeric ID automatically, use an empty stringID argument:
// Create a key such as Employee:8261.key:=datastore.NewKey(ctx,"Employee","",0,nil)// This is equivalent:key=datastore.NewIncompleteKey(ctx,"Employee",nil)
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:
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:
// Create Employee entityemployee:=&Employee{/* ... */}employeeKey,err:=datastore.Put(ctx,datastore.NewIncompleteKey(ctx,"Employee",nil),employee)// Use Employee as Address entity's parent// and save Address entity to datastoreaddress:=&Address{/* ... */}addressKey:=datastore.NewIncompleteKey(ctx,"Address",employeeKey)_,err=datastore.Put(ctx,addressKey,address)
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-03 UTC."],[[["\u003cp\u003eThis API supports first-generation runtimes and can be used when upgrading to corresponding second-generation runtimes, with a migration guide available for those updating to the App Engine Go 1.12+ runtime.\u003c/p\u003e\n"],["\u003cp\u003eEach entity in Datastore is uniquely identified by a key, which includes the entity's namespace, kind, an optional ancestor path, and an identifier that can either be a key name string or an integer numeric ID.\u003c/p\u003e\n"],["\u003cp\u003eIdentifiers for entities can be assigned either by specifying a key name string or by allowing Datastore to automatically assign an integer numeric ID.\u003c/p\u003e\n"],["\u003cp\u003eDatastore offers two auto ID policies, 'default' and 'legacy', to generate automatic IDs, and manual allocation is the preferred method for applications that need to display or depend on entity ID order.\u003c/p\u003e\n"],["\u003cp\u003eEntities in Cloud Datastore can form a hierarchy with parent-child relationships, where the ancestor path defines the lineage of an entity from a root entity, and the relationship between an entity and its parent is permanent.\u003c/p\u003e\n"]]],[],null,["# Creating and Using Entity Keys\n\n| This API is supported for first-generation runtimes and can be used when [upgrading to corresponding second-generation runtimes](/appengine/docs/standard/\n| go\n| /services/access). If you are updating to the App Engine Go 1.12+ runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/go-differences) to learn about your migration options for legacy bundled services.\n\n\u003cbr /\u003e\n\nEach entity in Datastore has a *key* that uniquely identifies it. The key consists of the following components:\n\n- The *namespace* of the entity, which allows for [multitenancy](/appengine/docs/legacy/standard/go111/multitenancy)\n- The [*kind*](#Kinds_and_identifiers) of the entity, which categorizes it for the purpose of Datastore queries\n- An optional [*ancestor path*](#Ancestor_paths) locating the entity within the Datastore hierarchy.\n- An [*identifier*](#Kinds_and_identifiers) for the individual entity, which can be either\n\n - a *key name* string\n - an integer *numeric ID*\n\nBecause the identifier is part of the entity's key, the identifier is associated\npermanently with the entity and cannot be changed. You assign the identifier in\neither of two ways:\n\n- Specify your own *key name* string for the entity.\n- Let Datastore automatically assign the entity an integer *numeric ID*.\n\n| Don't name a property \"key.\" This name is reserved for a special property used to store the Model key. Though it may work locally, a property named \"key\" will prevent deployment to App Engine.\n\nSpecifying a key name for an entity\n-----------------------------------\n\nTo assign a key name to an entity, provide a non-empty `stringID` argument to\n[`datastore.NewKey`](/appengine/docs/legacy/standard/go/datastore/reference#NewKey): \n\n // Create a key with a key name \"asalieri\".\n key := datastore.NewKey(\n \tctx, // context.Context\n \t\"Employee\", // Kind\n \t\"asalieri\", // String ID; empty means no string ID\n \t0, // Integer ID; if 0, generate automatically. Ignored if string ID specified.\n \tnil, // Parent Key; nil means no parent\n )\n\nTo let Datastore assign a numeric ID automatically, use an empty `stringID` argument: \n\n // Create a key such as Employee:8261.\n key := datastore.NewKey(ctx, \"Employee\", \"\", 0, nil)\n // This is equivalent:\n key = datastore.NewIncompleteKey(ctx, \"Employee\", nil)\n\n### Assigning identifiers\n\nYou can configure Datastore to generate auto IDs using [two different auto id policies](/appengine/docs/legacy/standard/go111/config/appref#app_yaml_Auto_ID_policy):\n\n- 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.\n- The `legacy` policy creates a sequence of non-consecutive smaller integer IDs.\n\nIf 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.\n| **Note:** Instead of using key name strings or generating numeric IDs automatically, advanced applications may sometimes wish to assign their own numeric IDs manually to the entities they create. Be aware, however, that there is nothing to prevent Datastore from assigning one of your manual numeric IDs to another entity. The only way to avoid such conflicts is to have your application obtain a block of IDs with the [`datastore.AllocateIDs`](/appengine/docs/legacy/standard/go/datastore/reference#AllocateIDs) function. Datastore's automatic ID generator will keep track of IDs that have been allocated with this function and will avoid reusing them for another entity, so you can safely use such IDs without conflict.\n\nUsing ancestor paths\n--------------------\n\nEntities in Cloud Datastore form a hierarchically structured space similar to\nthe directory structure of a file system. When you create an entity, you can\noptionally designate another entity as its *parent;* the new entity is a\n*child* of the parent entity (note that unlike in a file system, the parent\nentity need not actually exist). An entity without a parent is a *root\nentity.* The association between an entity and its parent is permanent, and\ncannot be changed once the entity is created. Cloud Datastore will never assign\nthe same numeric ID to two entities with the same parent, or to two root\nentities (those without a parent).\n\nAn entity's parent, parent's parent, and so on recursively, are its\n*ancestors;* its children, children's children, and so on, are its\n*descendants.* A root entity and all of its descendants belong to\nthe same *entity group.* The sequence of entities beginning with a root\nentity and proceeding from parent to child, leading to a given entity,\nconstitute that entity's *ancestor path.* The complete key identifying\nthe entity consists of a sequence of kind-identifier pairs specifying its\nancestor path and terminating with those of the entity itself: \n\n```\n[Person:GreatGrandpa, Person:Grandpa, Person:Dad, Person:Me]\n```\n\nFor a root entity, the ancestor path is empty and the key consists solely of\nthe entity's own kind and identifier: \n\n```\n[Person:GreatGrandpa]\n```\n\nThis concept is illustrated by the following diagram:\n\n\nTo designate an entity's parent, use the `parent` argument to\n[`datastore.NewKey`](/appengine/docs/legacy/standard/go/datastore/reference#NewKey). The value\nof this argument should be the parent entity's key.. The following example\ncreates an entity of kind `Address` and designates an `Employee` entity as its\nparent: \n\n // Create Employee entity\n employee := &Employee{ /* ... */ }\n employeeKey, err := datastore.Put(ctx, datastore.NewIncompleteKey(ctx, \"Employee\", nil), employee)\n\n // Use Employee as Address entity's parent\n // and save Address entity to datastore\n address := &Address{ /* ... */ }\n addressKey := datastore.NewIncompleteKey(ctx, \"Address\", employeeKey)\n _, err = datastore.Put(ctx, addressKey, address)"]]