简体   繁体   中英

Does the order of Appengine DataStore query conditions matter?

I must be missing something here, but this is a problem I can't seem to understand.

In the datastore, there is 1 record proj_id= u'1', type ='normal'

This (correctly) returns None.

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

This (correctly) returns the one record from the datastore.

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

This (correctly) returns None.

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

Yet, strangely, this returns the one record from the datastore, instead of None.

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

It seems like the order of query conditions matter. But why & how? Can you help?

You can't combine conditions with 'and' like that, you should pass them as separate arguments to query :

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

The behaviour you're seeing is due to how 'and' works in python:

The result of x and y is x if x is falsey, otherwise it is y .

The FilterNode returned by the equality operator (the result of self.type == 'normal' ) isn't falsey, so the result of self.type == 'normal' and self.proj_id == u'2' is just self.proj_id == u'2' - the query is only being passed one condition, and so the queries being performed in your 3rd and 4th code-blocks are exactly the same as in 1 and 2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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