google-app-engineentity-groups

Google Appengine: Odd get_by_key_name behavior


UPDATE: After further testing, it seems this issue affects all child entities in my entity group. My root parent for all these different instances is User kind, which is my own creation, not the built in User kind. After removing the parent=user from the constructor of the child Kind, the get_by_key_name works as expected. However, I would like to be able to use the Entity Group functionality along with the defined keys, if that is possible.

-- Hi, I am attempting to use defined key names for speedier querying in my GAE project.

However, I have run into an odd issue where I cannot fetch they key. This code does not seem to work:

for l in Logins.all().fetch():
    print Login.get_by_key_name(l.key().name())

Some notes:

So I have to ask, is there any obvious reasons why this would not work? Any known issues with key name searches using the SDK?


Solution

  • Your second comment is correct, AFAIK. The parent/child relationship is similar to a directory or folder structure in your filesystem. Your key is (conceptually) /parents/[parent_keyname]/logins/[login_keyname]. So if you try to fetch /logins/[login_keyname] you will not get your entity. (There is no rule that all Logins must be children of Parents; `get_by_key_name() must be told of the parent relationship every time.)

    In my own code, I have ended up building my keys myself with Key.from_path(). I use class methods, e.g. Login.key_for_name(some_parent, some_name) and also Login.get_by_key_name_for_parent(some_parent, some_name) (well, my method name is shorter but just making it clear. Then at least it is not possible for me to generate a key with the wrong parent/child relationship.