NDB 查询生成器未按预期工作

NDB Query builder doesn't work as expected

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

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()
query.filter(cls.taskgroup_id == taskgroup_id)
query.filter(cls.availability == True)
query.filter(cls.task_id > min_task_id)

它没有按预期工作。

当我运行[2],query formation分解成多个语句,它returns我一个实体,其availability为False,task_id等于min_task_id.

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

来自 Filtering by Property Values(强调我的):

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.

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

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