ThePiachu
ThePiachu

Reputation: 9185

What is Google App Engine's Datastore "parent key"?

What is the "parent key" used in Google App Engine's Datastore classes and what is it used for?

Upvotes: 3

Views: 4428

Answers (3)

Wooble
Wooble

Reputation: 89897

An entity in the datastore can optionally have a parent entity; the "parent key" is the key of the parent entity.

Originally (and still, in the master-slave datastore), transactions were only possible among entities in the same entity group, which is the set of entities with a common ancestor entity. In the HR datastore, cross-entity-group transactions are available, although only across a maximum of 5 entity groups.

So parent entities are used to create entity groups to be used in transactions. Note that having too-large entity groups can seriously hinder write speeds, since when writing to one entity in a group the entire group is essentially locked; trying to make too many writes to a single entity group results in datastore contention exemptions.

Upvotes: 4

sinemetu1
sinemetu1

Reputation: 1726

Reading over this would probably help you.

From the documentation:

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()
employee.put()

#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
address.put()​

Upvotes: 2

Adam Crossland
Adam Crossland

Reputation: 14213

Parent keys are used to establish entity groups. When one or more datastore entities share an ancestor, they are said to be part of the same entity group.

This comes into play in terms of how you write transactional datastore operations. If all of the entities to be modified are not part of the same entity group, you must specify that a Cross-group transaction is happening.

Upvotes: 2

Related Questions