Riley Lark
Riley Lark

Reputation: 20890

Will the automatic id allocator return ids I set arbitrarily on other entities?

If I arbitrarily set the id of a new entity to 1000, without using allocateIds first, could a later entity be automatically given the id 1000 and overwrite my original entity?

In pseudocode, if I have:

Entity e1;
e1.setId(1000);
datastore.put(e1);

... later...

Entity e2;
datastore.put(e2);

Is there any chance e2 will automatically be given the id 1000 and overwrite e1?

Upvotes: 3

Views: 263

Answers (1)

proppy
proppy

Reputation: 10504

As explained in the Java API documentation

Creating an entity for the purpose of insertion (as opposed to update) with this constructor is discouraged unless the id was obtained from a key returned by a KeyRange obtained from AsyncDatastoreService.allocateIds(String, long) or DatastoreService.allocateIds(String, long) for the same kind.

See also the Python documentation:

Be aware, however, that the datastore is not guaranteed to avoid app-assigned IDs. It is possible, though unlikely, that the datastore will assign a numeric ID that will cause a conflict with an entity with an app-assigned ID. The only way to avoid this problem is to have your app use allocate_ids() to obtain a batch of IDs. The datastore's automatic ID generator will not use IDs that have been obtained using allocate_ids(), so your app can use these IDs without conflict.

You should not be creating entity with ids that have not been allocated by the datastore, if you want to supply an user defined identifier as part of the Entity key, use key_name instead.

Upvotes: 3

Related Questions