Search code examples
google-cloud-platformgoogle-cloud-datastoreidempotent

Is saving by a specific entity id idempotent in Datastore?


Are entity id's idempotent when saving multiple entities with the same id?

I want to ensure only one entity of a specific id exists in my Datastore. I was thinking that I could give it a String id that I create (based off of other data) and if in the future the same entity gets saved again with the same id, the entity will just get overwritten (but two entities will not ever exists).

Am I correct in my thinking or does it not work that way?


Solution

  • An entity key uniquely identifies an Entity. So saving an entity with the same key, but different values is not idempotent, but results in only a single entity. The key for an entity can be thought of as a list of (Kind, name or id) tuples. name would be a string, and id an integer.

    In Objectify, if you use @Id with a string, then you'd access that part of the key with getName()