views:

156

answers:

3

I have a datastore object that uses one of the fields in the class to create the key. Suppose I have an object 'a' with a value 'k' for this field. If I try to add an object 'b' to the datastore, which also has value 'k' for the field, object 'b' overwrites object 'a'. Just wanted to confirm if this is expected behaviour. While it looks quite obvious, it would be great if someone can explain the underlying concept..

+1  A: 

A datastore object's Key is a unique value that identifies it, so yes, if object A and object B have the same Key in the datastore, they will overwrite each other.

Adam Crossland
+5  A: 

As Adam has explained, the short answer is that two entities can't share the same key (imagine a dict). The long answer though, is a little bit more complex. See the docs:

Paths and Key Uniqueness

The complete key of an entity, including the path, the kind and the name or numeric ID, is unique and specific to that entity. The complete key is assigned when the entity is created in the datastore, and none of its parts can change.

The keys of two different entities can have similar parts as long as at least one part is different. For instance, two entities can have the same kind and name if they have different parents. Similarly, two entities can have the same parent (or no parent) and name if they are of different kinds.

An application should not rely on numeric IDs being assigned in increasing order with the order of entity creation. This is generally the case, but not guaranteed.

jbochi
+1  A: 

Yes. The App Engine datastore doesn't distinguish between an 'insert' and an 'update' - both are a 'put' operation.

Nick Johnson