Hello everybody,

I've been tearing my hairs out trying to figure out how to achieve the 
following.


    Base = declarative_base()

    class Group(Base):
        g_id = Column(Integer, primary_key=True)
        events = relationship('Event', backref='group', lazy='dynamic')

    class Event(Base):
        e_id = Column(Integer, primary_key=True)
        g_id = Column(Integer, ForeignKey('group.g_id'), nullable=False)
        e_date = Column(Date, nullable=False)

Given above (simplified) model I can filter Group.events for single Group 
instance:

    group = ...
    events = group.events.filter(Event.e_date=='2013-01-01').all()

The question is how do I load all groups that had events on given date *with 
events property loaded with filtered Event objects*?

    session.query(Group).join(Event).filter(Event.e_date=='2013-01-01')

simply loads all groups that had events on given date but events had to be 
loaded on per-group object basis.

I know how to use custom query for relationship but that query and its bind 
parameters must be defined at the time the property is created:

    class Group(Base):
        today_events = relationship('Event', 
primaryjoin='and_(Group.g_id==Event.g_id, 
Event.e_date==func.current_date())')

What I need is something like above relationship with ability to provide *
primaryjoin* bind parameters at run-time. And that propert could be a 
read-only as far as I can load it in single query.

P.S. I'll try it like so now:

statement = session.query(Group, Event).filter(Event.e_date==...).subquery()
session.query(Group).from_statement(statement)

but that "smells" not so good and I'm not sure if that would even work.

- alex

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to