NDB查询生成器不能按预期工作 [英] NDB Query builder doesn't work as expected

查看:246
本文介绍了NDB查询生成器不能按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中有以下查询:
$ b $ p $ query $ cls.query()。filter(cls.taskgroup_id == taskgroup_id,cls (1)



<上面的工作正常。 (仅获取与taskgroup_id匹配的实体,并且可用,task_id> min_task_id)

但是,当我将查询分解为多个语句时。

  query = cls.query()
query.filter(cls.taskgroup_id == taskgroup_id)
query.filter(cls。可用性==真)
query.filter(cls.task_id> min_task_id)

它没有按预期工作。

当我运行[2]时,查询形成分解为多个语句,它返回一个实体,其可用性为False,并且task_id是等于min_task_id。

[2]不能按预期工作(或按我的预期)。我认为这里有一个用户错误。想知道它是什么。

按属性值过滤(重点是我的):


  query = Account.query(Account.userid> = 40,Account.userid< 50)

[b] [b]

不是在单个表达式中指定整个查询过滤器,而是
,您可能会发现它更多方便地逐步建立:例如:

appengine / standard / ndb / queries / snippets.py

  query1 = Account.query()#检索所有帐户实体
query2 = query1.filter(Account.userid> = 40)#在userid上过滤> = 40
query3 = query2.filter(Account.userid&l t; 50)#在用户ID上过滤< 50 too

query3 相当于 query 变量来自前面的
示例。 请注意,查询对象是不可变的,所以构造
query2 不会影响 query1 和构造 query3
不会影响 query1 query2 。


换句话说,对于你的例子, query.filter )语句实际上会修改查询



只需将语句结果分配给局部变量并使用它们,就像在引用的例子中一样。


I have the following query in my application

query = cls.query().filter(cls.taskgroup_id == taskgroup_id, cls.availability == True, cls.task_id > min_task_id).order(cls.task_id) query.fetch(1)

Above works fine as expected. (Fetches only those entities, which match taskgroup_id, and is available, and task_id > min_task_id)

However, when I break query into multiple statements.

query = cls.query()
query.filter(cls.taskgroup_id == taskgroup_id)
query.filter(cls.availability == True)
query.filter(cls.task_id > min_task_id)

It doesn't work as expected.

When I run [2], query formation broken down into multiple statements, it returns me a entity whose availability is False, and task_id is equal to min_task_id.

[2] doesn't work as expected (or as I expect). I think there is a user error here. Wondering what it is.

解决方案

From Filtering by Property Values (emphasis mine):

query = Account.query(Account.userid >= 40, Account.userid < 50)

[...]

Instead of specifying an entire query filter in a single expression, you may find it more convenient to build it up in steps: for example:

appengine/standard/ndb/queries/snippets.py

query1 = Account.query()  # Retrieve all Account entitites
query2 = query1.filter(Account.userid >= 40)  # Filter on userid >= 40
query3 = query2.filter(Account.userid < 50)  # Filter on userid < 50 too

query3 is equivalent to the query variable from the previous example. Note that query objects are immutable, so the construction of query2 does not affect query1 and the construction of query3 does not affect query1 or query2.

In other words for your example none of the query.filter() statements actually modifies query.

Just assign the results of the statements to local variables and use those instead, just as in the quoted example.

这篇关于NDB查询生成器不能按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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