GQL Not Recognising a ReferenceProperty in Filter

19 views
Skip to first unread message

G Heslop

unread,
Sep 28, 2011, 11:23:53 AM9/28/11
to google-a...@googlegroups.com
TL/DR: I am unable to get a reference property to be recognised as part of a query filter.

I have a datastore thats filled with ~850 groups and ~19,000 items. Each item may belong to only one group, I have two models in my app that represent a Group and an Item:

class Group(db.Model):
    Id = db.IntegerProperty()
    Name = db.StringProperty()
    # ... some other  properties

class Item(db.Model):
    Id = db.IntegerProperty()
    Name = db.StringProperty()
    Group = db.ReferenceProperty(Group, collection_name="groupItems")
    # ... some other properties

I can use the datastore admin to view a specific item (i.e. WHERE Id = 34) and see that it is connected correctly to a Group -

    SELECT * FROM Item WHERE Id = 34

This gives me a group with the following properties:

    Decoded entity key: Group: id=10321
    Entity key: agtzfmV2ZS1taW5lcnIMCxIFR3JvdXAY0VAM
    Id: 18

If I alter my GQL query to retrieve all items for this Group I get no results! -

    SELECT * FROM Item WHERE Group = KEY('agtzfmV2ZS1taW5lcnIMCxIFR3JvdXAY0VAM') -- No Results
    SELECT * FROM Item WHERE Group = KEY('Group', 'agtzfmV2ZS1taW5lcnIMCxIFR3JvdXAY0VAM') -- No Results

If I retrieve just the group, it works as expected -

    SELECT * FROM Group WHERE __key__ = KEY('agtzfmV2ZS1taW5lcnIMCxIFR3JvdXAY0VAM')  -- Returns 1 Group

This equally applies in my Python code. Calling:

    group = Group.gql("WHERE Id = :1", 18).get()
    items = Item.gql("WHERE Group = :1", group).fetch(50)

results in a list containing no items. Similarly

    group.groupItems.fetch(500) -- Returns no results

My question is - am I doing something particularly stupid? I have created a dummy project with a similar structure to prove to myself that it wasnt a naming problem (i.e. that Group wasn't a reserved word) and that returns just fine. (Attached if anyone is interested).

What am I doing wrong?

G Heslop

unread,
Sep 28, 2011, 12:11:41 PM9/28/11
to google-a...@googlegroups.com
nametest.zip
Reply all
Reply to author
Forward
0 new messages