NDB 查询构建器无法按预期工作 [英] NDB Query builder doesn't work as expected

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

问题描述

我的应用程序中有以下查询

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)

以上工作正常.(仅获取那些与 taskgroup_id 匹配且可用且 task_id > min_task_id 的实体)

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

query = cls.query()查询过滤器(cls.taskgroup_id == taskgroup_id)query.filter(cls.availability == True)查询过滤器(cls.task_id > min_task_id)

它没有按预期工作.

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

[2] 没有按预期(或我预期)工作.我认为这里存在用户错误.想知道它是什么.

解决方案

来自 按属性值过滤(强调我的):

<块引用>

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

[...]

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

appengine/standard/ndb/queries/snippets.py

query1 = Account.query() # 检索所有账户实体query2 = query1.filter(Account.userid >= 40) # 过滤用户id >= 40query3 = query2.filter(Account.userid <50) # 过滤用户 ID <50也

query3 等价于前面的 query 变量例子.注意查询对象是不可变的,所以构造query2 不影响 query1query3 的构造不影响 query1query2.

换句话说,对于您的示例,query.filter() 语句实际上都没有修改 query.

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

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