milesrichardson / ParsePy

A relatively up-to-date fork of ParsePy, the Python wrapper for the Parse.com API. Originally maintained by @dgrtwo
MIT License
516 stars 184 forks source link

exists query seems to give error #36

Closed Enalmada closed 10 years ago

Enalmada commented 10 years ago

When I try and use exists filter, I get bad type for $exists. I assume I should be doing exists='false'. Totally sorry if this is not supported or I am trying to use the exists filter wrong. I figured it would be either exists='false' or __exists=false based on my best reading of the docs.

I am trying to find all users with a publisherId of 15 that have LoggedIn as (undefined).

Sample...

users = list(User.Query.filter(publisherId='15').filter(LoggedIn__exists='false').limit(1000))

Error when I do __exists='false'

Traceback (most recent call last):
  File "./parse_user_update_test.py", line 36, in <module>
    users = list(User.Query.filter(publisherId='15').filter(LoggedIn__exists='false').limit(1000).order_by("LoggedIn", descending=False))
  File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/parse_rest/query.py", line 102, in __iter__
    return iter(self._fetch())
  File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/parse_rest/query.py", line 117, in _fetch
    return self._manager._fetch(**options)
  File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/parse_rest/query.py", line 41, in _fetch
    return [klass(**it) for it in klass.GET(uri, **kw).get('results')]
  File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/parse_rest/connection.py", line 108, in GET
    return cls.execute(uri, 'GET', **kw)
  File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/parse_rest/connection.py", line 102, in execute
    raise exc(e.read())
parse_rest.core.ResourceRequestBadRequest: {"code":102,"error":"bad type for $exists"}

Error when i do __exists=false

Traceback (most recent call last):
  File "./parse_user_update_test.py", line 36, in <module>
    users = list(User.Query.filter(publisherId='15').filter(LoggedIn__exists=false).limit(1000).order_by("LoggedIn", descending=False))
NameError: name 'false' is not defined

Raw example from REST API https://www.parse.com/docs/rest#queries-constraints

curl -X GET \
  -H "X-Parse-Application-Id: v8ZPeTW4nKEHDmmaU6eJUiF2s3tE16yrYZzGzipR" \
  -H "X-Parse-REST-API-Key: 3hT7widAIBwKx3N3vcqCVU9P0CN2WCCKZ16ex7vD" \
  -G \
  --data-urlencode 'where={"score":{"$exists":true}}' \
  https://api.parse.com/1/classes/GameScore

Thanks again for such a great library.

dgrtwo commented 10 years ago

Hi Enalmada: I can't quite reproduce the error yet, but let me ask: your traceback shows the line

users = list(User.Query.filter(publisherId='15').filter(publisherId__exists='true').limit(1000).order_by("LoggedIn", descending=False))

Why filter for publisherId existing after already filtering for publisherId equaling 15?

Enalmada commented 10 years ago

You have good eyes. I was actually manipulating my existing workaround code so I could provide you an exact stacktrace. As a workaround I found that if I sort by descending false, then all the non-exist fields go to the top but makes the code smell so I figured I would look harder into getting exists working. Thanks for your help on this.

Enalmada commented 10 years ago

So I updated the issue with my exact real code I am trying to run. I was trying to simplify the test case which usually is a good idea but I just realized that the error is different with my full intended code. Doh. Sorry. Hopefully this error is actually easier to reproduce.

dgrtwo commented 10 years ago

Change to __exists=False

Enalmada commented 10 years ago

Oh man you are right! Silly me. Thanks very much for helping me. I had a feeling I was doing something wrong on this one.

dgrtwo commented 10 years ago

No problem: In fact your query illuminated an important failure mode (filtering for a constraint after filtering for a specific value) that I just added an informative error message for in 728c492b8f8196e8a90a5a407e28b39e84818213. Thanks for your issue!