简体   繁体   English

NDB查询构建器无法按预期工作

[英]NDB Query builder doesn't work as expected

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) (仅获取与taskgroup_id匹配且可用且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]时,查询结构分解为多个语句,它返回一个实体,其可用性为False,而task_id等于min_task_id。

[2] doesn't work as expected (or as I expect). [2]不能按预期(或按我的预期)工作。 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 AppEngine上/标准/ NDB /查询/ 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. query3等效于上一个示例中的query变量。 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 . 请注意,查询对象是不可变的 ,因此query2的构造不影响query1 ,而query3的构造也不影响query1query2

In other words for your example none of the query.filter() statements actually modifies query . 换句话说,对于您的示例, query.filter()语句均未真正修改query

Just assign the results of the statements to local variables and use those instead, just as in the quoted example. 只需将语句的结果分配给局部变量,然后使用它们,就像引用的示例一样。

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

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