I recently moved entities from a namespace to another namespace and added a parent. Since then I noticed the following error appearing:
BadRequestError: the id allocated for a new entity was already in use, please try again: app: "p~appid"
name_space: "xyz"
path <
Element {
type: "Challenge"
name: "chal"
}
Element {
type: "Answer"
id: 0x14657000000000
}
>
After some research I came to the conclusion that this happens because the id generator generates numeric ids that already exist. Since I moved the entities to another path, a different id generator is used and that generator is not aware of existing ids.
I really want to prevent this as this results in unsuccessful requests. I found numerous solutions for the older db
api that involve allocating specific ids that are troublesome so the generator knows these exist. However I cannot seem to allocate a specific id for an ndb model.
I've tried to use retrying to retry putting a model but this also doesn't seem to help.
Is there any way to solve this?
I haven't tested this, but I think a solution is to use allocate_ids(1, parent=) and use the new_parent_key to create the actual key that you'll be using.
id = ndb.Model.allocated_ids(size=1, parent=old_parent_key)[0]
key = ndb.Key(cls, id, parent=new_parent_key).
So old_parent_key is the parent you used previously, and you create keys with the new_parent_key(that you're now using). As far as I'm aware, allocate_ids doesn't actually fetch the entities.
I haven't tested this, but I think a solution is to use allocate_ids(1, parent=old_parent_key) and use the new_parent_key to create the actual key that you'll be using.