Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datastore: Query does not allow inequality filter on __key__ property. #917

Closed
vinays opened this issue Jun 8, 2015 · 4 comments
Closed
Assignees
Labels
api: datastore Issues related to the Datastore API.

Comments

@vinays
Copy link

vinays commented Jun 8, 2015

Datastore query raises a ValueError for inequality(non-equality to be correct) operators on __key__ property.
https://github.com/GoogleCloudPlatform/gcloud-python/blob/3e86aaed3c7e192a17d9b790f90a41cc2b7baa6a/gcloud/datastore/query.py#L223

According to Google Cloud Datastore documentation inquality operators on __key__ property are supported, and should not raise exception.
https://cloud.google.com/datastore/docs/concepts/queries#Datastore_Key_filters

It is a critical part of our data-model.

@vinays
Copy link
Author

vinays commented Jun 8, 2015

For those who are facing the same issue; Workaround is to pass filters for __key__ property at the time of query construction.
Example:
Instead of:

query = datastore.Query('Kind')
query.add_filter('__key__', '>=', datastore.Key('Kind', '<startname>'))
query.add_filter('__key__', '<', datastore.Key('Kind', '<endname>'))
entities = query.fetch()

Use:

start_key = datastore.Key('Kind', '<startname>')
end_key = datastore.Key('Kind', '<endname>')
queries = datastore.Query('Kind', filters=[('__key__', '>=', start_key), ('__key__', '<', end_key)])
entities = query.fetch()

@dhermes
Copy link
Contributor

dhermes commented Jun 8, 2015

Thanks @vinays

I'm not sure why we limit in

        if property_name == '__key__':
            if not isinstance(value, Key):
                raise ValueError('Invalid key: "%s"' % value)
            if operator != '=':
                raise ValueError('Invalid operator for key: "%s"' % operator)

It also points out that the initial filters from the constructor aren't being verified.


I really appreciate it and will be making a fix ASAP!

dhermes added a commit to dhermes/google-cloud-python that referenced this issue Jun 8, 2015
Also checking in `Query` constructor that all the filters
are valid.

Fixes googleapis#917.
@vinays
Copy link
Author

vinays commented Jun 10, 2015

(thumbsup) @dhermes thanks for quick response and fix.

@dhermes
Copy link
Contributor

dhermes commented Jun 10, 2015

👍

@dhermes dhermes added the api: datastore Issues related to the Datastore API. label Dec 31, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: datastore Issues related to the Datastore API.
Projects
None yet
Development

No branches or pull requests

2 participants