Modeling-Relationship-in-App-Engine
Posted on 2013-11-11 21:15:27 +0900 in Technique
The origin article is in relationship.
The nature of relationships
There are many relationships, such as one to one, one to many and many to many. But the they are all built on the similar idea: reference. A reference is a field of an entity that contains the key of another entity.
Relationships in App Engine
One to many
In app engine, it is accomplished by storing the key of one side in the entity of many side.
To fetch all the pets owned by the owner, the query can be in this way.
To change the default name to our own name, code can be in the following way.
Another way to model the one to many relationships is to use parent entities. Every entity can specify a parent when it is created. The key of the parent forms part of the key of the created entity, and cannot be modified. The code is like the following:
The relationship is specified implicitly. It is the most import case for transactions: on app engine, transactions can only be operate on the same entity group. A entity group is the set with same parent.
One to one
A special case of one to many.
Many to many
The most obvious approach is the same as used in relationship databases: a join table.
Using the referenceproperty, the code is like the following:
Another approach is to have one side of the relationship store a list of the keys of entities on the other side of the relationship. This makes sense when the cardinality on one side is limited.