我使用gcloud-python库从云数据存储查询数据。把我的代码片段想象成这样
from google.appengine.ext import ndb
from datetime import datetime
class Order(ndb.Model):
order_name = ndb.StringProperty(required=True)
date_created = ndb.DateTimeProperty(default= datetime.now())
#code for querying the cloud datastore
from gcloud.datastore.query import Query
date_start = datetime.combine(date(year=2015, month=08, day=01), time())
date_end = datetime.combine(date(year=2015, month=08, day=03), time())
query = Query(kind='Order')
query.add_filter('order_name', '=', 'grand-line-order')
query.add_filter('date_created', '<', date_end)
query.add_filter('date_created', '>', date_start)
iterator = query.fetch(limit=10)
records, more, cursor = iterator.next_page()
print records
对于上面的代码片段,我得到
File "/Users/sathyanarrayanan/Desktop/app/services/cdr_helper.py", line 528, in fetch_cdr
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.
我的指数。Yaml文件是这样的
indexes:
- kind: Order
ancestor: yes
properties:
- name: date_created
- kind: Order
ancestor: yes
properties:
- name: date_created
direction: desc
- kind: Order
ancestor: yes
properties:
- name: order_name
direction: asc
- name: date_created
direction: desc
- kind: Order
ancestor: yes
properties:
- name: order_name
direction: asc
- name: date_created
direction: asc
我做错了什么吗?
所有的索引都使用ancestor:yes,所以应该在查询中添加祖先键。如果没有祖先,您的索引配置需要另一个带有'ancestor:no'的索引
- kind: Order
ancestor: no
properties:
- name: order_name
direction: asc
- name: date_created
direction: desc
注意:每个查询的特定索引
索引配置文档指出索引配置应该在一个名为datastore-indexes.xml
的XML文件中