views:

16

answers:

1

Hi Everyone,

I have an entity with a selfreferencyproperty and I wanted to search with a WHERE condition on the selfref field's key. My intent is to reduce DB hits by building a list of keys, then iterating over the same entity to build a nested list. I am using this list to hit memcache.get_multi() for a cached dictionary version of the resultsets I want to return. Here is a shortened version of the entitiy:

class Link(db.Model):     
    member = db.ReferenceProperty(Member)     
    url = db.TextProperty()   
    title = db.StringProperty()   
    category =db.SelfReferenceProperty()

I have built a "category" key dictionary based on another entity which stores access information. What I want to do is something like:

for categoryKey in accessDict:  
    categoryDBKey = db.Key(categoryKey)  
    q = db.GqlQuery('SELECT __key__ FROM Link WHERE category.key() = :1',categoryDBKey )  

for qItem in q:  
    cacheList.append('Link_' + str(qItem))  
    dataCache = memcache.get_multi(cacheList)

But I get the "BadQueryError: Parse Error: Invalid WHERE Condition" error.

I know I can just pass the whole category entity in:

for categoryKey in accessDict:  
    category = db.ket(db.Key(categoryKey))  
    q = db.GqlQuery('SELECT __key__ FROM Link WHERE category = :1',category)

but it seems like wasted RPCs if I already have the key value for the category I want to filter on.

Is there a way to use a WHERE condition on a referencyproperty's key?

Thanks

+2  A: 

Reference properties are stored as keys, so you can compare them directly:

for categoryKey in accessDict:  
    q = db.GqlQuery('SELECT __key__ FROM Link WHERE category = :1', db.Key(categoryKey))
Drew Sears
Thanks Drew, I must have tried every combination but the simplest one that worked.