Multiple data warehouse objects with the same identifier!

I have a huge problem - several objects in my data store of the same type have the same identifier! Their keys are Key s, but I assume that key.getId () will return a number that is unique among all objects of the same type.

Not this way! Different keys can return the same identifier! Aurgh! I can confirm this using a data viewer - multiple objects with the same value in the id / name column. Objects with duplicate keys are in different groups of entities - they have different parent keys. I have not seen repeated identifiers within the same group of entities, but I do not know that this cannot happen.

  • This is normal?
  • Is there a way to get a unique numeric identifier automatically generated? I understand that I can provide my own id values, but to implement my own system this seems extreme!
+7
google-app-engine google-cloud-datastore
source share
3 answers

The key contains not only the name of the identifier or key - it also includes the view, parent, and application identifier. This is why several objects can potentially share the same part of the key identifier - this is perfectly normal if the other key component is different.

If you want to create a unique identifier, use the db.allocate_ids method.

+5
source share

Objects using keys are identified in the data store (you can also see Java docs on keys , although I find Python docs explain a bit more). The article breakdown of storage is a bit outdated, but still useful.

The bottom line is that entities are identified by their full path to the data store, including the parent. Thus, in order to identify the child, you will also need to specify the parent identifier.

+1
source share

I have the same problem and I reached this topic. I had a problem with these identifiers because I used them to identify such objects in HTML, for example:

 <li id={{ entity.id }}>{{ entity.value }}</li> 

and I had duplicate identifiers on the DOM, but you answered me: it is likely that the data store will not repeat identifiers within the same parent group, as said in this part of the documentation :

"In other words, the object must have an identifier that is unique to all objects of the same type and with the same parent of the group of objects (if any),"

so I will generate an identifier based on the parent key-name (maybe an ID) and use this lazy number to identify the object in the DOM.

python:

 entity_rs = Entity.all() entities = [] for entity in entity_rs: entity.id = '%s-%s' % (entity.parent().key().id_or_name(), entity.key().id_or_name()) entities.append(entity): 

HTML:

 <ul> {% for entity in entities %} <li id="{{ entity.id }}">{{ entity.value }}</li> {% endfor %} </ul> 

I know this is not beautiful, but (under certain conditions) it works.

0
source share

All Articles