Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare DateTime and Interval in SQLAlchemy

I have this class:

class Monitor(db.Model):
    '''
    Base Monitor class.
    '''
    __tablename__ = 'monitor'
    id = db.Column(db.Integer(), primary_key=True)
    last_checked = db.Column(db.DateTime(timezone=False))
    poll_interval = db.Column(db.Interval(),
                              default=datetime.timedelta(seconds=300))

And I have this query where I attempt to return only objects that haven't been checked since (now - interval):

monitors = db.session.query(Monitor).\
           filter(or_(Monitor.last_checked < (datetime.utcnow() - Monitor.poll_interval)),
                      Monitor.last_checked == None).\
           all()

But the query returns nothing. I'm having a hard time figuring out the proper way to do this. Am I on the right track or am I missing something? I'm using MySQL as the database.

like image 768
Mark Avatar asked Jan 16 '13 20:01

Mark


1 Answers

Your parenthesis are wrong. I believe what you want is:

monitors = db.session.query(Monitor).\
       filter(or_(Monitor.last_checked < (datetime.utcnow() - Monitor.poll_interval),
                  Monitor.last_checked == None)).\
       all()
like image 92
J.L. Ennis Avatar answered Oct 26 '22 02:10

J.L. Ennis