【问题标题】:Querying Google Cloud datastore with ancestor not returning anything使用祖先查询 Google Cloud 数据存储不返回任何内容
【发布时间】:2017-10-20 09:54:53
【问题描述】:

我在 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

如何正确过滤父实体?

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 啊,谢谢!看来我完全错过了关于索引的部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 2012-04-14
    相关资源
    最近更新 更多