简体   繁体   English

Google Cloud Datastore:如何查询所有不同的父/祖先?

[英]Google Cloud Datastore: how to query all distinct Parent/Ancestor?

I have a Datastore Kind named Order , which has an ancestor/parent User . 我有一个名为Order的数据存储种类,它有一个祖先/父User

I'd like to query all distinct ancestors (users) of the orders using GQL, but the following query doesn't work. 我想使用GQL查询订单的所有不同祖先(用户),但是以下查询不起作用。

SELECT DISTINCT User FROM Order

The query's response is: 查询的响应是:

No entities matched this query. 没有实体符合此查询。

Make sure there are either simple or composite indexes for the properties you are searching. 确保要搜索的属性有简单索引或复合索引。 Learn more 学到更多

Since the parent is also part of the key, I also tried: 由于父母也是钥匙的一部分,所以我也尝试了:

SELECT DISTINCT __key__ FROM Order

But the error response said: 但是错误响应说:

GQL query error: Group by is not supported for the property: key GQL查询错误:属性: 不支持分组依据

You should note that the datastore ancestry is not established at the entity kind level: you can't really say that the Order kind has a User kind as ancestor. 您应该注意,数据存储体系不是在实体种类级别建立的:您不能真正说出Order 种类具有User 种类作为祖先。

The ancestry is established at the entity level - an entity has an ancestor only if one is specified at the entity creation level, otherwise it doesn't have one. 祖先是在实体级别建立的-仅当在实体创建级别指定一个祖先时,该实体才具有祖先,否则就没有祖先。 Also it doesn't matter what kind the ancestor entity is, different entities of the same kind can have ancestors of different kinds or no ancestors at all. 同样,祖先实体的种类也无关紧要,同一类型的不同实体可以具有不同种类的祖先,也可以根本没有祖先。

With this clarification in mind it sounds like each of your Order entities have a User entity as ancestor. 考虑到这一点,听起来每个Order实体都有一个User实体作为祖先。

The presence of an ancestry relationship places all related entities into the same entity group. 祖先关系的存在将所有相关实体置于同一实体组中。 All entities without an ancestry are each placed into their own entity group (they're entity group roots/leaders). 所有没有血统的实体都被放置在各自的实体组中(它们是实体组的根/领导者)。

In your case the Order entities are placed in the entity groups of their respective User entities. 在您的情况下, Order实体放置在其各自的User实体的实体组中。

When an ancestor query is made (ie either a specific ancestor or descendant entity is specified), the query results will be limited to the scope of that specific entity group only. 进行祖先查询时(即指定了特定祖先或后代实体),查询结果将仅限于该特定实体组的范围。 This allows such queries to be made in a transactional manner, with strongly consistent results. 这允许以事务方式进行此类查询,并获得非常一致的结果。

For an example of the syntax for an ancestor query see HAS ANCESTOR and HAS DESCENDANT clauses in google cloud datastore . 有关祖先查询的语法示例,请参阅Google Cloud Datastore中的HAS ANCESTOR和HAS DESCENDANT子句

The downside is that you can't make ancestor queries spanning multiple entity groups. 缺点是您无法进行跨越多个实体组的祖先查询。 In your case you're querying for User entities, which are in different entity groups. 在您的情况下,您要查询位于不同实体组中的User实体。 Even if you'd be placing all User entities in the same group (by specifying a common ancestor key for them) you'd still can't get what you want since you're looking for different ancestors by Order , which is a "descendant condition" - an Order can only have one User as ancestor. 即使您将所有User实体放在同一组中(通过为它们指定一个共同的祖先键),也仍然无法获得所需的内容,因为您要按Order查找不同的祖先。后代条件”- Order只能有一个User作为祖先。

This brings us to the root cause of your problem: you're using entity ancestry to model entity relationship. 这使我们发现了问题的根本原因:您正在使用实体祖先来建立实体关系模型。 That's not what the ancestry is designed for, it is designed for strong consistency. 这不是血统的目的,它是为实现高度一致性而设计的。 I know, it sounds confusing. 我知道,这听起来令人困惑。

What you can do is forget about the datastore ancestry and use plain key properties to model your relationships, with no restrictions. 您可以做的就是忘记数据存储的祖先,并使用无格式键属性无限制地为您的关系建模。 See also E-commerce Product Categories in Google App Engine (Python) 另请参阅Google App Engine(Python)中的电子商务产品类别

I'd add an order_count property to the User kind and a user key property to the Order kind. 我要为User类型添加一个order_count属性,为Order类型添加一个user密钥属性。 Whenever an order is created I'd create an Order entity with its user property set to the respective User entity key and I'd increment the order_count property of that User entity. 每当创建Order我都会创建一个Order实体,并将其user属性设置为相应的User实体键,然后增加该User实体的order_count属性。 Then, to get what you want, you simply need to query for User entities which have a non-zero order_count . 然后,要获得所需的内容,只需要查询具有非零order_count User实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM