Reputation: 313
I have a Google Cloud Platform project with Datastore configured in Firestore mode, where the following (Python) code produces a google.api_core.exceptions.FailedPrecondition: 400 no matching index found.
error.
from google.cloud import datastore
store = datastore.Client(project='my-project')
query = store.query(kind='my-kind')
query.add_filter('start_date', '<', 1500000000)
results = query.fetch()
for r in results: # error thrown here
print(r)
As Firestore automatically indexes single fields (and I have not excluded the start_timestamp
field from this automatic indexing) I would expect this to work. What am I doing wrong?
(Note that this works perfectly on another project where Datastore is in Datastore rather than Firestore mode; the project I happen to need to use unfortunately already has Firestore in place. As Firestore is supposed to be backwards-compatible with Datastore, I had hoped this would just work.)
Upvotes: 0
Views: 1554
Reputation: 1872
As Jim Morrison indicated, you need to use the Firestore library.
from google.cloud import firestore
db = firestore.Client()
# [START get_simple_query]
docs = db.collection(u'my-collection').where(u'start_date', u'<=', 1500000000).stream()
for doc in docs:
print(u'{} => {}'.format(doc.id, doc.to_dict()))
Upvotes: 1
Reputation: 2887
Unfortunately, indexes are different between Firestore native & Firestore in Datastore mode. You'll need to use the matching client to get your queries to work. I suggest you use the google-cloud-firestore python libraries instead of the google-cloud-datastore libraries.
Upvotes: 0