jamesaharvey
jamesaharvey

Reputation: 14291

How to retrieve Google App Engine entities by ancestor

I have the following 2 models in my Google App Engine datastore:

class Search(db.Model):
    what = db.StringProperty()

class SearchResult(db.Model):
    search = db.ReferenceProperty(Search)

    title = db.StringProperty()
    content = db.StringProperty()

And, I am trying to retrieve all SearchResult entities for a given Search entity in the following function:

def get_previous_search_results(what='', where=''):
    search_results = None

    search = db.GqlQuery("SELECT * FROM Search WHERE what = :1", what).fetch(1)
    if search:
        search_results = db.GqlQuery("SELECT * FROM SearchResult WHERE ANCESTOR IS :1", search[0].key()).fetch(10)

    return search_results

However, it always returns an empty set.

Any ideas what I am doing wrong? I've read through the Python Datastore API docs and this seem like the correct way to do this, but it's not working.

Upvotes: 1

Views: 617

Answers (1)

Wooble
Wooble

Reputation: 90027

Are you creating the Search entities with a parent? The ReferenceProperty doesn't create an ancestor relationship, and it seems likely you might want search.searchresult_set, which will be a Query for SearchResult objects that have a reference to the Search object 'search'.

Upvotes: 4

Related Questions