无法执行不同的投影查询
Can't execute a distinct projection query
我有一个简单的小"Observation"class:
from google.appengine.ext import ndb
class Observation(ndb.Model):
remote_id = ndb.StringProperty()
dimension_id = ndb.IntegerProperty()
metric = ndb.StringProperty()
timestamp_observed = ndb.StringProperty()
timestamp_received = ndb.DateTimeProperty(auto_now_add=True)
@classmethod
def query_book(cls):
return cls.query()
我可以 运行 将针对数据存储的查询投影到 return 仅某些列。例如:
observations = Observation.query().fetch(projection=[Observation.dimension_id])
效果很好,但我只想要独特的结果。 documentation 让这听起来很简单:
# Functionally equivalent
Article.query(projection=[Article.author], group_by=[Article.author])
Article.query(projection=[Article.author], distinct=True)
但是当我这样做时:
observations = Observation.query().fetch(projection=[Observation.dimension_id], group_by=[Observation.dimension_id])
observations = Observation.query().fetch(projection=[Observation.dimension_id], distinct=True)
两种变体都出现错误。
TypeError: Unknown configuration option ('group_by')
TypeError: Unknown configuration option ('distinct')
这发生在 localhost 和 prod 环境中。我错过了什么?
愚蠢的我 - 所有这些参数都需要位于 query() 函数中,而不是 fetch() 中。投影元素实际上在 fetch() 中起作用,但您需要将投影和不同的参数都移到 query() 中才能使其起作用。
来自Grouping:
Projection queries can use the distinct
keyword to ensure that only
completely unique results will be returned in a result set. This will
only return the first result for entities which have the same values
for the properties that are being projected.
Article.query(projection=[Article.author], group_by=[Article.author])
Article.query(projection=[Article.author], distinct=True)
Both queries are equivalent and will produce each author's name only
once.
希望这对遇到类似问题的其他人有所帮助:)
我有一个简单的小"Observation"class:
from google.appengine.ext import ndb
class Observation(ndb.Model):
remote_id = ndb.StringProperty()
dimension_id = ndb.IntegerProperty()
metric = ndb.StringProperty()
timestamp_observed = ndb.StringProperty()
timestamp_received = ndb.DateTimeProperty(auto_now_add=True)
@classmethod
def query_book(cls):
return cls.query()
我可以 运行 将针对数据存储的查询投影到 return 仅某些列。例如:
observations = Observation.query().fetch(projection=[Observation.dimension_id])
效果很好,但我只想要独特的结果。 documentation 让这听起来很简单:
# Functionally equivalent
Article.query(projection=[Article.author], group_by=[Article.author])
Article.query(projection=[Article.author], distinct=True)
但是当我这样做时:
observations = Observation.query().fetch(projection=[Observation.dimension_id], group_by=[Observation.dimension_id])
observations = Observation.query().fetch(projection=[Observation.dimension_id], distinct=True)
两种变体都出现错误。
TypeError: Unknown configuration option ('group_by')
TypeError: Unknown configuration option ('distinct')
这发生在 localhost 和 prod 环境中。我错过了什么?
愚蠢的我 - 所有这些参数都需要位于 query() 函数中,而不是 fetch() 中。投影元素实际上在 fetch() 中起作用,但您需要将投影和不同的参数都移到 query() 中才能使其起作用。
来自Grouping:
Projection queries can use the
distinct
keyword to ensure that only completely unique results will be returned in a result set. This will only return the first result for entities which have the same values for the properties that are being projected.Article.query(projection=[Article.author], group_by=[Article.author]) Article.query(projection=[Article.author], distinct=True)
Both queries are equivalent and will produce each author's name only once.
希望这对遇到类似问题的其他人有所帮助:)