Michael Bayer wrote:
[snip]
> Whats missing here is the context.  

I want to define ORM relations in the mapper that I can access without 
having to do a manual join.

Normally you'd do this:

mapper(A, a_table,
        properties={
           'bs': relation(B , backref='a',
                 primaryjoin=and_(a_table.c_id == b_table.c.a_id,
                                  b_table.c.status == 'FOO'),
        })


But I'd like to automate this:

mapper(A, a_table,
        properties={
           'bs': my_own_relation(B , backref='a'),
        })

my_own_relation behaves like relation, except it adds an extra clause 
restricting the query, say, b_table.c.status == 'FOO'. It should have 
access to the parent and child tables so it can do this generically.

I've tried to accomplish this by overriding _determine_joins and 
manipulating self.primaryjoin after the default is set up, but I think 
that breaks because backrefs have already been set up earlier. I've 
hacked around that now I think, but it's not very pretty.

> From what you describe, "use the
> primaryjoin and amend with an extra filter", that is just:
> 
> query(Parent).join(Parent.children).filter(Child.foo==bar)

This presents a potential alternative implementation strategy where this 
is implemented using properties on the model classes that do something 
like this.

Regards,

Martijn


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to