Entities, Properties, and Keys
Data objects in the 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.)
The Datastore supports a variety of data types for property values . These include, among others:
- Integers
- Floating-point numbers
- Strings
- Dates
- Binary data
Each entity in the 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 the 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 the 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 the Datastore.
The 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.
Contents
- Kinds and identifiers
- Ancestor paths
- Transactions and entity groups
- Properties and value types
- Working with entities
- 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 the 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',
last_name='Salieri')
employee.hire_date = datetime.datetime.now().date()
employee.attended_hr_training = True
employee.put()
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 the 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 the 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.