views:

72

answers:

1

I've got a an app where I'm storing posts and their authors. Very straightforward each post has one author model.

The problem is this: I fetch the last 10 posts using one call, using fetch() with limit = 10. But when I print them out, GAE uses 10 extra gets to access the author details, because the author object is a reference property on the post... a classic n+1 query scenario - 1 query to get 10 posts and then 10 queries to get each author.

Is there any way I can iterate over my posts to collect just the author object keys, so I can load them all up at one go using

db.get(all_author_keys)
+1  A: 

See my response to this question just a couple hours ago -- a Q almost identical by amazing coincidence by this one, though by a different poster.

In short, to do this, use the get_value_for_datastore of the Property object.

Alex Martelli
Lol... yes. I vote to close this question as a duplicate.
Sudhir Jonathan
@Sudhir, as the Q's author, you can just delete it any time you want -- just hit the link for that!
Alex Martelli
No, I'm assuming the next person who has this question might search for it phrased this way, with terms like 'n+1', 'collect keys' etc. So it should probably just stay and link to the other one.
Sudhir Jonathan
@Sudhir, btw, just curious -- why accept and **not** upvote too? That's really peculiar behavior on SO [[no big deal for me, as I max out on rep every day anyway;-) -- I'm just keen to support normal SO etiquette or understand the specific reasons for departing from it!-). So tx in advance for explaining _why_.]]
Alex Martelli
:) No firm reason. Retrospectively, I've marked the answer as right, but its up to other people to say if they think its good for me. At least in my head. That's why I upvoted on the answer to the other question... I'm recommending that answer to him, but I'll leave it to others to recommend this answer to me.
Sudhir Jonathan