google-cloud-python icon indicating copy to clipboard operation
google-cloud-python copied to clipboard

PreconditionFailed: 412 no matching index found. when using inequality filters

Open sourcepirate opened this issue 11 years ago • 6 comments

I am using appengine to run my app and gcloud for accessing cloud datastore. I have gcloud model where i am storing username and date_time attribute.

but when i am doing this.

from gcloud.datastore.query import Query
from datetime import datetime
query = Query(kind="MyTable")
query.add_filter('username', '=', 'somevalue')
query.add_filter('date_time', '<', datetime.now())

iterator = query.fetch(limit=10)
records, more, cursor = iterator.next_page()
print records

I am getting the following exception.

 File "/Users/sathyanarrayanan/Desktop/app/services/cdr_helper.py", line 528, in fetch_last_users
    records, more, cursor = iterator.next_page()
  File "/Users/sathyanarrayanan/Desktop/app/gcloud/datastore/query.py", line 388, in next_page
    transaction_id=transaction and transaction.id,
  File "/Users/sathyanarrayanan/Desktop/app/gcloud/datastore/connection.py", line 257, in run_query
    datastore_pb.RunQueryResponse)
  File "/Users/sathyanarrayanan/Desktop/app/gcloud/datastore/connection.py", line 108, in _rpc
    data=request_pb.SerializeToString())
  File "/Users/sathyanarrayanan/Desktop/app/gcloud/datastore/connection.py", line 85, in _request
    raise make_exception(headers, content, use_json=False)
PreconditionFailed: 412 no matching index found.

Below is my index file.

#File index.yaml

- kind: MyTable
  ancestor: yes
  properties:
  - name: date_time

- kind: MyTable
  ancestor: yes
  properties:
  - name: date_time
    direction: desc

- kind: MyTable
  ancestor: yes
  properties:
  - name: username
    direction: asc
  - name: date_time
    direction: desc

- kind: MyTable
  ancestor: yes
  properties:
  - name: username
    direction: asc
  - name: date_time

for more details: http://stackoverflow.com/questions/32607351/412-no-matching-index-found-while-executing-a-query-in-cloud-datastore

sourcepirate avatar Sep 18 '15 06:09 sourcepirate

What indexes do you see when you look at your project in the Google Developer's Console? E.g., https://console.developers.google.com/project/your-project-123/datastore/indexes

tseaver avatar Sep 18 '15 15:09 tseaver

Thanks for teaching me about datetime.datetime.combine!

I'll agree with @tseaver and say that you should double check that your indexes have built and are ready to serve for your project.

Here is a suggestion to make sure your indexes are correct. Make the queries with ndb using the GAE local server (was called dev_appserver, now done via gcloud preview app run app.yaml) and then the GAE local server will automatically generate the index.yaml file you need for those queries.

dhermes avatar Sep 18 '15 15:09 dhermes

@tseaver I see the indexes which i already mentioned on the file and it was serving , And it was the first thing i checked after getting this error.

sourcepirate avatar Sep 19 '15 03:09 sourcepirate

@plasmashadow I executed the following snippet in the dev server:

import datetime
from google.appengine.ext import ndb


class MyTable(ndb.Model):
    username = ndb.StringProperty()
    date_time = ndb.DateTimeProperty(auto_now=True)


parent_key = ndb.Key(MyTable, 'parent')
MyTable(parent=parent_key, username='somevalue').put()
MyTable(parent=parent_key, username='somevalue2').put()
MyTable(parent=parent_key, username='somevalue3').put()

q = MyTable.query(MyTable.username == 'somevalue',
                  MyTable.date_time < datetime.datetime.utcnow(),
                  ancestor=parent_key)
print(q.fetch())

The dev server automatically generated the following:

- kind: MyTable
  ancestor: yes
  properties:
  - name: username
  - name: date_time

If you are actually storing keys with ancestors, you'll need to use the Query.ancestor setter.

dhermes avatar Sep 19 '15 04:09 dhermes

thanks guys, it really worked.

sourcepirate avatar Sep 24 '15 06:09 sourcepirate

Great to hear!

dhermes avatar Sep 24 '15 14:09 dhermes