3
votes

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?

1

1 Answers

5
votes

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.