Are datastore Ids unique across all namespaces?

248 views
Skip to first unread message

NP

unread,
Jun 14, 2019, 12:19:24 AM6/14/19
to Google App Engine
If I store an entity in the datastore within the context of a specific namespace, is it possible that the same id will occur in another namespace?

For example if I have   - 

Key(MODEL_NAME, 123456789, namespace=NS_1)

, is it possible for the datastore to also create a record such as

Key(MODEL_NAME, 123456789, namespace=NS_2)

where the id - 123456789 is appearing in more than 1 namespace?


Note that Datastore is the one automatically generating the ids.

Harmit Rishi (Cloud Platform Support)

unread,
Jun 14, 2019, 10:06:48 PM6/14/19
to google-a...@googlegroups.com
In order to effectively address your question, I would like to take a moment and explain the characteristics of a Entity. Each entity within Datastore has a key that uniquely identifies it. The key (as you provided) consists of the following components: 

1. Namespace of the entity
2. The kind of entity (categorized for queries)
3. An Identifier for the individual entity (can be a key name string or a integer numeric id.)

I will be assuming that you are referring to the integer numeric id for your question. 

As far as having two of the same numeric ID's goes, indeed it is possible and this can be done by having your application assign their own numeric IDs manually to the entities being created. However, as indicated in the documentation here, "there is nothing to prevent a Datastore mode database from assigning one of your manual numeric IDs to another entity". You will have to implement the allocateids() method within your application which will allow you to obtain a block of IDs in order to avoid the conflict mentioned above.

Also, since you mentioned that "Datastore is the one automatically generating the ids", the documentation found here states that the "automatic ID generator will keep track of IDs that have been allocated" and that Datastore "will avoid reusing them for another entity".

Therefore to sum it all up. Yes, you can achieve the goal you want. However, it would be a good idea to implement the best practices for it as well such as: manually assigning Ids with implementing allocateids()

I hope this helps!

NP

unread,
Jun 14, 2019, 10:53:30 PM6/14/19
to Google App Engine
Thanks for your response. And yes, I was referring to the numeric id.

In case it wasn't clear from my post, I actually want the ids to be unique. I was just trying to confirm that it is.

The reason I would prefer the ids are unique is because I have seeded data which all my users can use. The users can then in turn create entities for their application which are saved with a namespace for the user. Since each user basically has access to 2 namespaces (the default or blank namespace which contains my seeded data and the namespace for their app), I would like to search for entities by just their numeric ids. If I find the record, I then check the namespace associated with the corresponding key. For this to work, the ids need to be unique across all namespaces.



On Friday, June 14, 2019 at 7:06:48 PM UTC-7, Harmit Rishi (Cloud Platform Support) wrote:
In order to effectively address your question, I would like to take a moment and explain the characteristics of a Entity. Each entity within Datastore has a key that uniquely identifies it. The key (as you provided) consists of the following components: 

1. Namespace of the entity
2. The kind of entity (categorized for queries)
3. An Identifier for the individual entity (can be a key name string or a integer numeric id.)

I will be assuming that you are referring to the integer numeric id for your question. 

As far as having two of the same numeric ID's goes, indeed it is possible and this can be done by having your application assign their own numeric IDs manually to the entitles being created. However, as indicated in the documentation here, "there is nothing to prevent a Datastore mode database from assigning one of your manual numeric IDs to another entity". You will have to implement the allocateids() method within your application which will allow you to obtain a block of IDs in order to avoid the conflict mentioned above.

Nicolas (Google Cloud Platform Support)

unread,
Jun 17, 2019, 3:55:36 PM6/17/19
to Google App Engine

Hi,

 

I understand your use case and I think the best way to accomplish that would be something like suggested in this StackOverflow post and set up your own  GUIDs.

 

I hope that helps!


NP

unread,
Jun 17, 2019, 11:05:39 PM6/17/19
to Google App Engine
Thanks.

That link answered my question with the answer being - No, the Ids are not unique across all namespaces. The link also provided a solution for me if I need them to be unique.

Nicolas (Google Cloud Platform Support)

unread,
Jun 18, 2019, 12:28:33 PM6/18/19
to Google App Engine

I’m glad to know we were able to help you,

 

Have a great day!


Reply all
Reply to author
Forward
0 new messages