使用祖先的投影查询抛出错误 400

Projection query using ancestor throws error 400

正在通过执行以下操作将以下数据添加到数据存储区:

    key = ds.key(
            'User', 'alice',
            'id'
        )

    entity = datastore.Entity(
        key=key,
    )

    entity.update({"data": "big amount of information"})
    entity.update({"property_name": "confidential"})
    ds.put(entity)

然后,为了减少资源使用,我尝试使用投影查询只获取小属性并忽略 "data",它通过执行以下操作确实很大:

    key = ds.key(
        'User', 'alice'
    )
    query = ds.query(ancestor=key)
    query.projection = ["property_name"]

    entities = list()

    for entity in query.fetch():
        entities.append(entity)

    return entities

但是我得到这个错误:

google.api_core.exceptions.InvalidArgument: 400 Unable to plan or invalidate query.

Ancestor 查询需要复合索引,即使您正在投影单个 属性。确保你已经做到了。