【问题标题】:Does the order of Appengine DataStore query conditions matter?Appengine DataStore 查询条件的顺序是否重要?
【发布时间】:2014-05-17 15:15:38
【问题描述】:

我一定在这里遗漏了什么,但这是一个我似乎无法理解的问题。

在数据存储中,有 1 条记录 proj_id= u'1', type ='normal'

这(正确)返回无。

delete_keys = self.query(self.proj_id == u'2').fetch(keys_only=True)

这(正确)从数据存储中返回一条记录。

delete_keys = self.query(self.type == 'normal').fetch(keys_only=True)

这(正确)返回无。

delete_keys = self.query(self.type == 'normal' and self.proj_id == u'2').fetch(keys_only=True)

然而,奇怪的是,这会从数据存储中返回一条记录,而不是 None。

delete_keys = self.query(self.proj_id == u'2' and self.type == 'normal').fetch(keys_only=True)

似乎查询条件的顺序很重要。但是为什么以及如何?你能帮忙吗?

【问题讨论】:

    标签: python google-app-engine google-cloud-datastore


    【解决方案1】:

    您不能像这样将条件与“和”组合,您应该将它们作为单独的参数传递给query

    delete_keys = self.query(self.type == 'normal', self.proj_id == u'2').fetch(keys_only=True)
    

    您看到的行为是由于“和”在 python 中的工作方式:

    如果x 为假,x and y 的结果为x,否则为y

    相等运算符返回的FilterNode(self.type == 'normal'的结果)不是假的,所以self.type == 'normal' and self.proj_id == u'2'的结果只是self.proj_id == u'2' - 查询只传递了一个条件,所以查询是在您的第 3 和第 4 个代码块中执行的操作与在 1 和 2 中执行的完全相同。

    【讨论】:

    • 也可以使用ndb查询AND运算符ndb.AND(self.type == 'normal',self.proj_id == u'2')
    猜你喜欢
    • 2011-06-02
    • 2011-03-10
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多