PreconditionFailed: 412 no matching index found. when using inequality filters
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
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
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.
@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.
@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.
thanks guys, it really worked.
Great to hear!