5

I have a user in my system who has created an entity which I'd like to retrieve. I'm attempting to do this using a filter because it's supposed to be faster than a call to the gql method. However, the filter returns no results and gql works.

randy_res = Vote.all().filter('created_by=', randy).fetch(limit=10)
randy_res = Vote.gql('WHERE created_by=:1', randy)

Is there any reason why the filter would return an empty list and the gql call would return the proper results?

Matt Norris
  • 8,596
  • 14
  • 59
  • 90
  • 3
    As an aside, the speed difference between using gql and filters to build the query should be extremely trivial compared to the overhead of actually executing the query. If gql is more readable or more intuitive for you, stick with it. – Drew Sears Jul 22 '10 at 01:56
  • 3
    As much as I dislike GQL, @Drew is right: Any performance difference is trivial. – Nick Johnson Jul 22 '10 at 10:18

1 Answers1

12

When using filter(), you are required to have a space between the field name and the operator. To get your filter() call to work as intended, you just need to insert a space before the equal sign:

randy_res = Vote.all().filter('created_by =', randy).fetch(limit=10)
David Underhill
  • 15,896
  • 7
  • 53
  • 61
  • 1
    ahhhhhh! this made me go insane for a night. Silent failures are my worst nightmare. I filed an issue 2 years ago, but it doesn't look like anyone else cares (only 6 stars). http://code.google.com/p/googleappengine/issues/detail?id=688 – Peter Recore Jul 22 '10 at 01:29
  • 1
    @Peter Unfortunately, it's not a bug: The original code is a perfectly valid query for the property with name 'created_by=', and any change would break this (admittedly fairly uncommon) behaviour. – Nick Johnson Jul 22 '10 at 10:19
  • 2
    sigh. I know it's not a bug in the strict sense. I do like calling it a "wart" in the api, as ryanb put it in the issues log. :) Maybe I should look into creating a rule in findbugs or eclipse that would give me a warning when it sees the lack of a space. – Peter Recore Jul 22 '10 at 16:07
  • 1
    Agreed. It might be helpful to highlight this behavior in the docs. The docs always use a space before the operator, but I don't remember seeing any tips highlighting the importance of this space. – David Underhill Jul 22 '10 at 19:20
  • this made me go crazy too, yesterday. But @nick's explanation is reasonable – Axarydax Dec 20 '10 at 11:47