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:
- I have only tested in the SDK
l.ke开发者_运维知识库y().name() Returns the key name string listed with the entity when I look in the data store. I can copy and paste the string out of the data story and use that as the arg to get_by_key_name() and that does not work either.
keynames for the Login kind are all prefixed with an "l" (i.e. lowercase "L") and are other wise all lowercase and may contain underscores or dashes but are under 500 bytes.
Other kind serches like this work.
- The key is a interpolation of 2 properties of the Login kind, and I can fetch the objects just fine using regular .filter() methods
- The "parent" for the instances is a User class. (mentioning in case this has some bearing on the way I have to fetch)
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?
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 Login
s must be children of Parent
s; `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.
精彩评论