Confusion about __key__ and key_name, and sort order

12 views
Skip to first unread message

bsb

unread,
Nov 15, 2009, 9:08:43 AM11/15/09
to Google App Engine
I have the following code:

entities = db.GqlQuery("SELECT __key__ FROM mymodel").fetch(300)
keys = {}
while entities:
for key in entities:
if key in keys.keys():
keys[key] = keys[key] + 1
else:
keys[key] = 1
entities = db.GqlQuery("SELECT __key__ FROM mymodel WHERE __key__
> :1", entities[-1]).fetch(300)

Now, the thing that I don't get is that when I look at the "keys"
dictionary above, I get something like that:

agxhcHBjYWRkeWxpdmVyHgsSD01vZGVsX21vYmlsZWFwcCIJMzM4Njk0OTU0DA 128
agxhcHBjYWRkeWxpdmVyHgsSD01vZGVsX21vYmlsZWFwcCIJMzM4NzI2NTg4DA 139
agxhcHBjYWRkeWxpdmVyHgsSD01vZGVsX21vYmlsZWFwcCIJMjg0OTQ4OTg1DA 1

ie. some keys are fetched multiple times from the db? How is that
possible? I must be making a simple mistake, but I don't find it. I
read that all queries without exlicit ordering are ordered by __key__
asc by default, so that shouldn't be the problem. What's wrong with my
logic here?!

I should say that all mymodel objects are created with

obj = mymodel( key_name = 'some_unique_id', [...] )

if that's of any significance.

Thanks in advance,
Benjamin

bsb

unread,
Nov 15, 2009, 9:28:10 AM11/15/09
to Google App Engine
Just to say, the following query gives me the correct result:

entities = mymodel.all().order('myId').fetch(300)
keys = {}
while entities:
for obj in entities:
if obj.myId in keys.keys():
keys[obj.myId] = keys[obj.myId] + 1
else:
keys[obj.myId] = 1
entities = mymodel.all().order('myId').filter('myId >', entities
[-1].myId).fetch(300)

So why does the key-based query not work?

bsb

unread,
Nov 15, 2009, 10:12:36 AM11/15/09
to Google App Engine
To add to that, I tried to use the download_data tool to get a csv
dump of my datastore entities. It runs fine, until eventually it hits
this:

[ERROR ] [Thread-11] ExportProgressThread:
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/
GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/
google/appengine/tools/bulkloader.py", line 1385, in run
self.PerformWork()
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/
GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/
google/appengine/tools/bulkloader.py", line 2127, in PerformWork
item.progress_key = self.db.StoreKeys(item.key_start,
item.key_end)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/
GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/
google/appengine/tools/bulkloader.py", line 1926, in StoreKeys
repr(key_start), repr(key_end))
AssertionError: datastore_types.Key.from_path(u'Model_mobileapp',
u'286532250', _app_id_namespace=u'appcaddylive') not less than
datastore_types.Key.from_path(u'Model_mobileapp', u'284152154',
_app_id_namespace=u'appcaddylive')

so, something seems a bit broken there. If it helps, I have the
following indexes defined for Model_mobileapp:

indexes:
- kind: Model_mobileapp
properties:
- name: __key__
direction: desc

Because the bulkloader used to complain that it needs a descending
index on key for parallel downloads.

风笑雪

unread,
Nov 16, 2009, 1:22:27 AM11/16/09
to google-a...@googlegroups.com
You need sort by key:
keys = db.GqlQuery("SELECT __key__ FROM mymodel ORDER BY __key__").fetch(300)
keys = db.GqlQuery("SELECT __key__ FROM mymodel WHERE __key__ > :1
ORDER BY __key__", entities[-1]).fetch(300)

2009/11/15 bsb <b...@pearcomp.com>:
> --
>
> You received this message because you are subscribed to the Google Groups "Google App Engine" group.
> To post to this group, send email to google-a...@googlegroups.com.
> To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-appengine?hl=.
>
>
>
Reply all
Reply to author
Forward
0 new messages