开发者

Multiple datastore entities with the same ID!

I've got a huge problem - multiple entities in my datastore of the same kind have the same id! Their keys are Keys, but I have been assuming that key.getId() will return a number th开发者_StackOverflow中文版at is unique among all entities of the same kind.

Not so! Different keys can return the same id! Aurgh! I can confirm this by using the data viewer - multiple entities with the same value in the id/name column. The entities with repeat keys are all in different entity groups - they have different parent keys. I have not seen any repeat ids within the same entity group, but I do not know that that cannot happen.

  1. Is this normal?
  2. Is there any way to get a unique numeric identifier automatically generated? I understand that I could supply my own id values, but having to implement my own system for this seems extreme!


A key includes more than just the id or key name - it also includes the kind, parent(s), and app ID too. This is why multiple entities could potentially share the same ID portion of the key - it's perfectly fine as long as some other component of the key differs.

If you want to generate a unique id, use the db.allocate_ids method.


The datastore identifies entities using keys (you can also see the Java docs on keys, though I believe the Python docs explain a bit more). The article on storage breakdown is a bit out-of-date, but still useful too.

The bottom line is that entities are identified by their full path in the datastore, this includes the entity's parent. So to identify the child, you will need to include the parent's id as well.


I've got the same problem and reached this topic. I was having a problem with these IDs because I used them to identify the entities of this kind in the HTML, example:

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

and I was having duplicate ids on the DOM, but you gave me the answer: probably the datastore will not repeat IDs within the same parent group, as said on this part of the documentation:

"In other words, an object must have an ID that is unique across all objects of the same kind and with the same entity group parent (if any)",

so I'll generate an id based on the parent named-key (could be an ID) and use this lazy-generated number to identify the entity on 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 it's not beautiful, but (under certain conditions) it works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜