Jason O.
Jason O.

Reputation: 3300

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?

Upvotes: 0

Views: 72

Answers (1)

Greg
Greg

Reputation: 10360

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.

Upvotes: 2

Related Questions