I have the following model:
class Product(ndb.Model):
name = ndb.StringProperty()
bidTime = ndb.DateTimeProperty()
price = ndb.IntegerProperty()
...
I'd likd to use the following query:
productRanks = Product.query(Product.bidTime>=startDate,
Product.bidTime<endDate).order(-Product.price).fetch()
where startDate and endDate are datetime objects. But I got the following error message:
The first sort property must be the same as the property to which the inequality filter is applied
If I add Product.bidTime in the order then there will be no error:
.order(Product.bidTime, -Product.price)
However, the sorted result would be wrong (according to date, not price). So, what is the problem?
There is no problem as far as appengine is concerned. It is behaving as documented. From the docs
Note: Because of the way the App Engine Datastore executes queries, if a query specifies inequality filters on a property and sort orders on other properties, the property used in the inequality filters must be ordered before the other properties.
See https://developers.google.com/appengine/docs/python/datastore/queries#Sort_Orders
You may need to sort in memory after you get your result set.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With