python-2.7google-app-enginegqlquery

gqlQuerry comparing datetime objects


Here is the relevant code copied from my application on GAE.

today = datetime.datetime.strptime(date_variable, "%d/%m/%Y")
yesterday = ref_today - datetime.timedelta(days=1)
tomorrow = ref_today + datetime.timedelta(days=1)
logging.info('%s  :  %s  :  %s', yesterday, today, tomorrow)
#2016-02-19 00:00:00  :  2016-02-20 00:00:00  :  2016-02-21 00:00:00

records = db.GqlQuery("SELECT * FROM ProgrammeQueue"
         " WHERE scheduledFrom < :1 AND scheduledFrom > :2 "
         " ORDER BY scheduledFrom DESC", 
         tomorrow, yesterday)

Problem Statement : Output: all records of 19/02/2016 and 20/02/2016 Expected: records = all records of 20/02/2016

What am I doing wrong ?


Solution

  • You query states:

    WHERE scheduledFrom < :tomorrow AND scheduledFrom > :yesterday
    

    where tomorrow and yesterday are datetimes. the time is set to 00:00:00, so the results will include dates of 19/02/2016 where the time is greater than 00:00:00.

    maybe your query should be rewritten to use date objects not datetime objects (depending on your model definition). or maybe you need to rewrite it to something like this:

    records = db.GqlQuery("SELECT * FROM ProgrammeQueue"
             " WHERE scheduledFrom < :1 AND scheduledFrom >= :2 "
             " ORDER BY scheduledFrom DESC", 
             tomorrow, today)