查询Google Cloud数据存储区,祖先不返回任何内容

时间:2017-10-20 09:54:54

标签: python google-app-engine google-cloud-datastore

我在Google App Engine上使用google cloud datastore library灵活的环境。

我有一个run实体,我用它作为pathway个实体的父级:

ds = datastore.Client('project-name')
parent = ds.query(kind='run', order=('-timestamp',)).fetch(1)
parent = list(parent)[0]

print(parent.key)    # <Key('run', 1), project=project-name>

如果我获取一些pathway个实体,它们似乎有正确的父

pathways = ds.query(kind='pathway', order=('-timestamp',)).fetch(limit=10)

for pathway in pathways:
    print(pathway.key.parent)    # <Key('run', 1), project=project-name>

但如果我尝试像这样过滤父母:

pathways = ds.query(kind='pathway', ancestor=parent.key, order=('-timestamp',)).fetch(limit=10)

然后我收到错误:

google.api.core.exceptions.PreconditionFailed
google.api.core.exceptions.PreconditionFailed: 412 no matching index found. recommended index is:
- kind: pathway
  ancestor: yes
  properties:
- name: timestamp
    direction: desc

如何正确过滤父实体?

1 个答案:

答案 0 :(得分:2)

为了满足某些查询,Cloud Datastore需要构建索引。详细信息在此处定义:https://cloud.google.com/datastore/docs/concepts/indexes

定义建议的索引应该使有问题的查询有效。