为什么标准NDB查询与GQL查询相比返回不同的结果?

时间:2014-04-21 19:40:17

标签: python google-app-engine app-engine-ndb gql

我使用以下代码来计算实体数量:

def get_emails_count(email_type=None, yesterday=None, status=None):
    emails = EmailQueue.query()
    if email_type:
        emails = emails.filter(EmailQueue.email_type == email_type)
    if yesterday:
        ...
    if status:
        emails = emails.filter(EmailQueue.status == status)
    emails = emails.count(keys_only=True)
    return emails

get_emails_count(email_type=4, status=0)

以下GQL查询(在GAE控制台中使用):

SELECT __key__ FROM EmailQueue WHERE email_type=4 and status=0

第一个代码返回245,第二个代码返回5个实体(看起来更正确)。第一个代码出了什么问题?

1 个答案:

答案 0 :(得分:1)

小心使用隐式无比较:

if status:
  emails = emails.filter(EmailQueue.status == status)

请注意,if 0:将评估为False。相反,你应该写:

if status is not None:
  emails = emails.filter(EmailQueue.status == status)