Entities, Properties, and Keys

Data objects in App Engine Datastore are known as entities. An entity has one or more named properties, each of which can have one or more values. Entities of the same kind need not have the same properties, and an entity's values for a given property need not all be of the same data type. (If necessary, an application can establish and enforce such restrictions in its own data model.)

Datastore supports a variety of data types for property values. These include, among others:

  • Integers
  • Floating-point numbers
  • Strings
  • Dates
  • Binary data

For a full list of types, see Properties and value types.

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 identifier for the individual entity, which can be either
    • a key name string
    • an integer numeric ID
  • An optional ancestor path locating the entity within Datastore hierarchy

An application has access only to entities it has created itself; it can't access data belonging to other applications. It can fetch an individual entity from Datastore using the entity's key, or it can retrieve one or more entities by issuing a query based on the entities' keys or property values.

The Python App Engine SDK includes a data modeling library for representing Datastore entities as instances of Python classes, and for storing and retrieving those instances in Datastore.

Datastore itself does not enforce any restrictions on the structure of entities, such as whether a given property has a value of a particular type; this task is left to the application and the data modeling library.


  1. Kinds and identifiers
  2. Ancestor paths
  3. Transactions and entity groups
  4. Properties and value types
  5. Working with entities
    1. Creating an entity
    2. Retrieving an entity
    3. Updating an entity
    4. Deleting an entity
    5. Batch operations
    6. Deleting entities in bulk via the Administration Console
  6. Understanding write costs

Kinds and identifiers

Each Datastore entity is of a particular kind, which categorizes the entity for the purpose of queries: for instance, a human resources application might represent each employee at a company with an entity of kind Employee. In the Python Datastore API, an entity's kind is determined by its model class, which you define in your application as a subclass of the data modeling library class db.Model. The name of the model class becomes the kind of the entities belonging to it. All kind names that begin with two underscores (__) are reserved and may not be used.

The following example creates an entity of kind Employee, populates its property values, and saves it to Datastore:

import datetime
from google.appengine.ext import db

class Employee(db.Model):
  first_name = db.StringProperty()
  last_name = db.StringProperty()
  hire_date = db.DateProperty()
  attended_hr_training = db.BooleanProperty()

employee = Employee(first_name='Antonio',

employee.hire_date = datetime.datetime.now().date()
employee.attended_hr_training = True


The Employee class declares four properties for the data model: first_name, last_name, hire_date, and attended_hr_training. The Model superclass ensures that the attributes of Employee objects conform to this model: for example, an attempt to assign a string value to the hire_date attribute would result in a runtime error, since the data model for hire_date was declared as db.DateProperty.

In addition to a kind, each entity has an identifier, assigned when the entity is created. Because it is part of the entity's key, the identifier is associated permanently with the entity and cannot be changed. It can be assigned in either of two ways:

  • Your application can specify its own key name string for the entity.
  • You can have Datastore automatically assign the entity an integer numeric ID.

To assign an entity a key name, provide the named argument key_name to the model class constructor when you create the entity:

# Create an entity with the key Employee:'asalieri'.
employee = Employee(key_name='asalieri')

To have Datastore assign a numeric ID automatically, omit the key_name argument:

# Create an entity with a key such as Employee:8261.
employee = Employee()

Assigning identifiers

The Datastore can be configured to generate auto IDs using two different auto id policies:

  • The default policy generates a random sequence of 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.

Datastore generates a random sequence of IDs that are approximately uniformly distributed. Each ID can be up to 16 decimal digits long.

Ancestor paths

Entities in the 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. The 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:


This concept is illustrated by the following diagram:

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 following example creates an entity of kind Address and shows two ways of designating an Employee entity as its parent:

# Create Employee entity
employee = Employee()

# Set Employee as Address entity's parent directly...
address = Address(parent=employee)

# ...or using its key
e_key = employee.key()
address = Address(parent=e_key)

# Save Address entity to datastore

Transactions and entity groups

Every attempt to create, update, or delete an entity takes place in the context of a transaction. A single transaction can include any number of such operations. To maintain the consistency of the data, the transaction ensures that all of the operations it contains are applied to Datastore as a unit or, if any of the operations fails, that none of them are applied.

As mentioned above, an entity group is a set of entities connected through ancestry to a common root element. Only entities in the same entity group can be read in a consistent manner with respect to one another, and only if the reader uses an ancestor query. This means that if two entities are in different entity groups, it is not possible to read them in such a way that a transaction affecting both entities would be guaranteed to be visible on both entities or neither entity. Also, a single transaction can span at most 25 entity groups. These properties of the Datastore tend to make it desirable to put entities together in a single entity group. However, there is a write throughput limit of about one transaction per second within a single entity group because Datastore uses masterless, synchronous replication over a wide geographic area. Therefore, it is important to consider both the data consistency and write throughput requirements of your application when designing your Datastore schema.

In many applications, it is acceptable to use eventual consistency (i.e. a non-ancestor query spanning multiple entity groups, which may at times return slightly stale data) when obtaining a broad view of unrelated data, and then to use strong consistency (an ancestory query, or a get of a single entity) when viewing or editing a single set of highly related data. In such applications, it is usually a good approach to use a separate entity group for each set of highly related data.

Properties and value types

The data values associated with an entity consist of one or more properties. Each property has a name and one or more values. A property can have values of more than one type, and two entities can have values of different types for the same property. Properties can be indexed or unindexed (queries that order or filter on a property P will ignore entities where P is unindexed).

The following value types are supported:

Value type Python type(s) Sort order Notes
Integer int
Numeric 64-bit integer, signed
Floating-point number float Numeric 64-bit double precision,
IEEE 754
Boolean bool False<True
Text string (short) str
(str treated as ASCII)
Up to 500 Unicode characters
Text string (long) db.Text None Up to 1 megabyte

Not indexed
Byte string (short) db.ByteString Byte order Up to 500 bytes
Byte string (long) db.Blob None Up to 1 megabyte

Not indexed
Date and time datetime.date
Geographical point db.GeoPt By latitude,
then longitude
Postal address db.PostalAddress Unicode
Telephone number db.PhoneNumber Unicode
Email address db.Email Unicode
Google Accounts user users.User Email address
in Unicode order
Instant messaging handle db.IM Unicode
Link db.Link Unicode
Category db.Category Unicode
Rating db.Rating Numeric
Datastore key db.Key By path elements
(kind, identifier,
kind, identifier...)
Blobstore key blobstore.BlobKey Byte order
Null NoneType None

Important: We strongly recommend that you do not store a UserProperty, since it includes the email address and the user's unique ID. If a user changes their email address and you compare their old, stored User to the new User value, they won't match.

For text strings and unencoded binary data (byte strings), Datastore supports two value types:

  • Short strings (up to 500 Unicode characters or bytes) are indexed and can be used in query filter conditions and sort orders.
  • Long strings (up to 1 megabyte) are not indexed and cannot be used in query filters and sort orders.
Note: The long byte string type is named Blob in the Datastore API. This type is unrelated to blobs as used in the Blobstore API.

When a query involves a property with values of mixed types, Datastore uses a deterministic ordering based on the internal representations:

  1. Null values
  2. Fixed-point numbers
    • Integers
    • Dates and times
    • Ratings
  3. Boolean values
  4. Byte strings (short)
  5. Unicode strings
    • Text strings (short)
    • Postal addresses
    • Telephone numbers
    • Email addresses
    • IM handles
    • Links
    • Categories
  6. Floating-point numbers
  7. Geographical points
  8. Google Accounts users
  9. Datastore keys
  10. Blobstore keys

Because long text strings and long byte strings are not indexed, they have no ordering defined.

Working with entities

Applications can use the Datastore API to create, retrieve, update, and delete entities. If the application knows the complete key for an entity (or can derive it from its parent key, kind, and identifier), it can use the key to operate directly on the entity. An application can also obtain an entity's key as a result of a Datastore query; see the Datastore Queries page for more information.

Creating an entity

In Python, you create a new entity by constructing an instance of a model class, populating its properties if necessary, and calling its put() method to save it to Datastore. You can specify the entity's key name by passing a key_name argument to the constructor:

employee = Employee(key_name='asalieri',

employee.hire_date = datetime.datetime.now().date()
employee.attended_hr_training = True


If you don't provide a key name, Datastore will automatically generate a numeric ID for the entity's key:

employee = Employee(first_name='Antonio',

employee.hire_date = datetime.datetime.now().date()
employee.attended_hr_training = True


Retrieving an entity

To retrieve an entity identified by a given key, pass the Key object as an argument to the db.get() function. You can generate the Key object using the class method Key.from_path(). The complete path is a sequence of entities in the ancestor path, with each entity represented by its kind (a string) followed by its identifier (key name or numeric ID):

address_k = db.Key.from_path('Employee', 'asalieri', 'Address', 1)
address = db.get(address_k)

db.get() returns an instance of the appropriate model class. Be sure that you have imported the model class for the entity being retrieved.

Updating an entity

To update an existing entity, modify the attributes of the object, then call its put() method. The object data overwrites the existing entity. The entire object is sent to the Datastore with every call to put().

To delete a property, delete the attribute from the Python object:

del address.postal_code

then save the object.

Deleting an entity

Given an entity's key, you can delete the entity with the db.delete() function

address_k = db.Key.from_path('Employee', 'asalieri', 'Address', 1)

or by calling the entity's own delete() method:

employee_k = db.Key.from_path('Employee', 'asalieri')
employee = db.get(employee_k)

# ...


Batch operations

The db.put(), db.get(), and db.delete() functions (and their asynchronous counterparts db.put_async(), db.get_async(), and db.delete_async()) can accept a list argument to act on multiple entities in a single Datastore call:

# A batch put.
db.put([e1, e2, e3])

# A batch get.
entities = db.get([k1, k2, k3])

# A batch delete.
db.delete([k1, k2, k3])

Batch operations do not change your costs. You will be charged for every key in a batched operation, whether or not each key exists. The size of the entities involved in an operation does not affect the cost.

Deleting entities in bulk via the Administration Console

You can use the Datastore Admin tab of the Administration Console to delete all entities of a given kind, or all entities of all kinds, in the default namespace. To enable this feature, include the builtin handler datastore_admin in your app.yaml file:

- datastore_admin: on

This enables the Datastore Admin screen in the Data section of the Administration Console. From this screen, you can select the entity kind(s) to delete individually or in bulk, and delete them using the Delete Entities button. Note that bulk deletion takes place within your application, and thus counts against your quota.

Understanding write costs

When your application executes a Datastore put operation, Datastore must perform a number of writes to store the entity. Your application is charged for each of these writes. You can see how many writes will be required to store an entity by looking at the data viewer in the SDK Development Console. This section explains how these write costs are calculated.

Every entity requires a minimum of two writes to store: one for the entity itself and another for the built-in EntitiesByKind index, which is used by the query planner to service a variety of queries. In addition, Datastore maintains two other built-in indexes, EntitiesByProperty and EntitiesByPropertyDesc, which provide efficient scans of entities by single property values in ascending and descending order, respectively. Each of an entity's indexed property values must be written to each of these indexes.

As an example, consider an entity with properties A, B, and C:

Key: 'Foo:1' (kind = 'Foo', id = 1, no parent)
A: 1, 2
B: null
C: 'this', 'that', 'theOther'

Assuming there are no composite indexes (see below) for entities of this kind, this entity requires 14 writes to store:

  • 1 for the entity itself
  • 1 for the EntitiesByKind index
  • 4 for property A (2 for each of two values)
  • 2 for property B (a null value still needs to be written)
  • 6 for property C (2 for each of three values)

Composite indexes (those referring to multiple properties) require additional writes to maintain. Suppose you define the following composite index:

Kind: 'Foo'
A ▲, B ▼

where the triangles indicate the sort order for the specified properties: ascending for property A and descending for property B. Storing the entity defined above now takes an additional write to the composite index for every combination of A and B values:

(1, null) (2, null)

This adds 2 writes for the composite index, for a total of 1 + 1 + 4 + 2 + 6 + 2 = 16. Now add property C to the index:

Kind: 'Foo'
A ▲, B ▼, C ▼

Storing the same entity now requires a write to the composite index for each possible combination of A, B, and C values:

(1, null, 'this') (1, null, 'that') (1, null, 'theOther')

(2, null, 'this') (2, null, 'that') (2, null, 'theOther')

This brings the total number of writes to 1 + 1 + 4 + 2 + 6 + 6 = 20.

If a Datastore entity contains many multiple-valued properties, or if a single such property is referenced many times, the number of writes required to maintain the index can explode combinatorially. Such exploding indexes can be very expensive to maintain. For example, consider a composite index that includes ancestors:

Kind: 'Foo'
A ▲, B ▼, C ▼
Ancestor: True

Storing a simple entity with this index present takes the same number of writes as before. However, if the entity has ancestors, it requires a write for each possible combination of property values and ancestors, in addition to those for the entity itself. Thus an entity defined as

Key: 'GreatGrandpa:1/Grandpa:1/Dad:1/Foo:1' (kind = 'Foo', id = 1, parent = 'GreatGrandpa:1/Grandpa:1/Dad:1')
A: 1, 2
B: null
C: 'this', 'that', 'theOther'

would require a write to the composite index for each of the following combinations of properties and ancestors:

(1, null, 'this', 'GreatGrandpa') (1, null, 'this', 'Grandpa') (1, null, 'this', 'Dad') (1, null, 'this', 'Foo')

(1, null, 'that', 'GreatGrandpa') (1, null, 'that', 'Grandpa') (1, null, 'that', 'Dad') (1, null, 'that', 'Foo')

(1, null, 'theOther', 'GreatGrandpa') (1, null, 'theOther', 'Grandpa') (1, null, 'theOther', 'Dad') (1, null, 'theOther', 'Foo')

(2, null, 'this', 'GreatGrandpa') (2, null, 'this', 'Grandpa') (2, null, 'this', 'Dad') (2, null, 'this', 'Foo')

(2, null, 'that', 'GreatGrandpa') (2, null, 'that', 'Grandpa') (2, null, 'that', 'Dad') (2, null, 'that', 'Foo')

(2, null, 'theOther', 'GreatGrandpa') (2, null, 'theOther', 'Grandpa') (2, null, 'theOther', 'Dad') (2, null, 'theOther', 'Foo')

Storing this entity in Datastore now requires 1 + 1 + 4 + 2 + 6 + 24 = 38 writes.