无法执行明确的投影查询 [英] Can't execute a distinct projection query

查看:147
本文介绍了无法执行明确的投影查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Observation类:

  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):
返回cls.query()

我可以针对数据存储运行投影查询以仅返回特定列。例如:

  observations = Observation.query()。fetch(projection = [Observation.dimension_id])
code>

这很好用,但我只想要独特的结果。 文档使这种声音变得简单:

 #功能相当于
Article.query(投影= [Article.author],group_by = [Article.author])
Article.query(projection = [Article.author],distinct = True)

但是当我这样做时:

  observation = Observation.query()。fetch(projection = [Observation.dimension_id],group_by = [Observation.dimension_id] )
observation = Observation.query()。fetch(projection = [Observation.dimension_id],distinct = True)


$

$ p $ TypeError:未知配置选项('group_by')
$ b

b TypeError:未知配置选项('distinct')

这也发生在localhost和prod环境中。我错过了什么?

解决方案

傻傻的我 - 所有这些params需要坐在query()函数内,而不是在fetch ()。投影元素实际上在fetch()中工作,但您需要将投影和独立参数都移动到query()中才能使用。

分组


投影查询可以使用 distinct 关键字来确保结果集中只返回
完全唯一的结果。这将
仅返回具有相同值
的实体的第一个结果,以用于正在投影的属性。

  Article.query(projection = [Article.author],group_by = [Article.author])
Article.query(projection = [Article.author],distinct = True)

这两个查询都是等价的,每个作者的名字只会产生一次

blockquote>

希望这可以帮助任何有类似问题的人:)

I have a simple little "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()

I can run projection queries against the Datastore to return only certain columns. E.g:

observations = Observation.query().fetch(projection=[Observation.dimension_id])

This works nicely, but I only want unique results. The documentation makes this sound easy:

# Functionally equivalent
Article.query(projection=[Article.author], group_by=[Article.author])
Article.query(projection=[Article.author], distinct=True)

But when I do this:

observations = Observation.query().fetch(projection=[Observation.dimension_id], group_by=[Observation.dimension_id])
observations = Observation.query().fetch(projection=[Observation.dimension_id], distinct=True)

I get errors for both variants.

TypeError: Unknown configuration option ('group_by')
TypeError: Unknown configuration option ('distinct')

This happens on localhost and in the prod environment too. What am I missing?

解决方案

Silly me - all of these params need to sit within the query() function, not within fetch(). The projection elements actually works in fetch(), but you need to move both the projection and distinct arguments into query() to get it to work.

From 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.

Hope this helps anyone else with a similar problem :)

这篇关于无法执行明确的投影查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆