412在云数据存储区

时间:2015-09-16 11:29:35

标签: google-cloud-datastore gcloud-python google-cloud-python

我正在使用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 quering 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.

我的Index.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

我做错了吗?请帮帮我。

2 个答案:

答案 0 :(得分:1)

使用祖先的所有索引:是的,因此应在查询中添加祖先密钥。如果没有祖先,您的索引配置需要另一个带有&#39;祖先的索引:否&#39;

- kind: Order
  ancestor: no
  properties:
    - name: order_name
      direction: asc
    - name: date_created
      direction: desc

注意:每个查询的特定索引

答案 1 :(得分:0)

index configuration docs表示索引配置应位于名为SELECT SUM(e1.ENT_LOCAL_AMOUNT+e1.ENT_DISCRETIONARY_AMOUNT) AS "Entitlement" FROM CLAIM_PERIODS cp1 JOIN ENTITLEMENTS e1 ON cp1.CPE_REFNO=e1.ENT_CPE_REFNO WHERE e1.ENT_REFNO=(SELECT MAX(to_number(e2.ENT_REFNO)) FROM ENTITLEMENTS e2 WHERE e1.ENT_CPE_REFNO=e2.ENT_CPE_REFNO AND e2.ENT_END_DATE in ( {?HB_As_At_Date}) AND e2.ENT_CREATED_DATE in ({?HB_As_At_Date}) AND cp1.CPE_CPA_CPY_CODE='HB' 的XML文件中。