Search code examples
pythonsqlalchemyrelationship

Current Date in SQLAlchemy ORM relationship


I'm trying to configure a relationship between two objects based on the current date. Say I have a Person object and a relationship to a bunch of Event objects. If the Event object holds a DateTime (start) on it, I want to make a relationship to all of today's events.

So far I have:

class Person:
    id = Column(Integer, primary_key=True)
    todays_events = relationship('Event', primaryjoin='and_(Person.id == Event.person_id, cast(Event.start, Date) == "2016-04-23"')

This works but I can't find what I need to replace the date string with "2016-04-23" to get the equivalent of CURDATE().

Does anyone know what I'm looking for?


Solution

  • Found the answer right after posting... of course.

    func.current_date()

    so:

    class Person:
         id = Column(Integer, primary_key=True)
         todays_events = relationship('Event', primaryjoin='and_(Person.id == Event.person_id, cast(Event.start, Date) == func.current_date()')