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

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

问题描述

我有一个简单的观察"类:

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)

但是当我这样做时:

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')

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

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

推荐答案

我傻了——所有这些参数都需要放在 query() 函数中,而不是在 fetch() 中.投影元素实际上在 fetch() 中工作,但您需要将投影和不同的参数移动到 query() 中才能使其工作.

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.

来自分组:

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

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天全站免登陆