Vincent
Vincent

Reputation: 1157

GAE Python - get_by_key_name no result - maybe Parent issue?

I use a simple Model.get_by_key_name('item_key') to retrieve an item from the datastore. This all worked fine until today, whereas now the query is returning None, although I can confirm that there is an existing entity with that key name.

As far as I know, the only thing I have changed to the model today, is adding a parent upon creation. I'm not very familiar with parent/ancestor. Could this influence the way I have to call the entity? What else can I try to figure out what is causing this?

Upvotes: 0

Views: 117

Answers (1)

Alex Martelli
Alex Martelli

Reputation: 881675

Indeed, per https://cloud.google.com/appengine/docs/python/datastore/modelclass#Model_get_by_key_name , the get_by_key_name function must be told about the parent, if any -- the parent alters the actual key used in the store.

Specifically, the signature is:

Model.get_by_key_name (key_names, parent=None)

(you can actually pass multiple key names as long as the entities have the same parent). The default is parent=None which worked when you were getting entities without a parent -- but now that the parent is there, it must be specified in the call.

The key includes the kind of entity (here that's supplied by the specific subclass of Model you're calling the method on), the id or name, the parent, and the namespace (no need to worry about the last bit unless you're explicitly using namespaces:-).

Upvotes: 2

Related Questions