Sachin Sukumaran
Sachin Sukumaran

Reputation: 715

Google Appengine datastore - Not able to do an order by with different column

I have a table with 5 fields. I did a query with one of the column and when I tried to do order by another column it is not allowing. This is my basic structure

class jobs(ndb.Model):
    category        = ndb.StringProperty()
    title           = ndb.StringProperty()
    content         = ndb.TextProperty()
    last_date_apply = ndb.DateTimeProperty()
    date_of_posting = ndb.DateTimeProperty()

    current_ts = datetime.datetime.now() + datetime.timedelta(seconds = 5.5*60*60)
    job             = jobs()
    query1          = job.query(jobs.last_date_apply >= current_ts).order(jobs.date_of_posting)

Here I am trying to get the data with last_date_apply greater than current ts and , it should be in the order of date_of_posting . The error clearly mentioned as "BadRequestError: The first sort property must be the same as the property to which the inequality filter is applied. In your query the first sort property is date_of_posting but the inequality filter is on last_date_apply"

How can I do a query, in such a way that get only with condition last_date_apply >= current_ts and I need that to be in a sorted order based on date_of_posting. If it is not allowing how to a work around. I can do the reverse , But in that I think number of reads will be more .

Upvotes: 0

Views: 48

Answers (1)

cwlau
cwlau

Reputation: 428

You will need to sort the field last_date_apply first.

query1 = job.query(jobs.last_date_apply >= current_ts).order(jobs.last_date_apply, jobs.date_of_posting)

# OR

query1 = job.query(jobs.last_date_apply >= current_ts).order(jobs.last_date_apply).order(jobs.date_of_posting)

Attaching a reference link to the documentation: https://cloud.google.com/appengine/docs/standard/python/ndb/queries#order

Upvotes: 1

Related Questions