I have a table structure something like this:

* Two tables Foo and Bar
* FooBar, an many-to-many association table between Foo and Bar
* A "County" lookup table referenced by both Foo and Bar
* A "Region" lookup table referenced by County

I'm frequently running queries that involve both Foo and bar, and 
County/Region tables joined to both of them.  Since I need to reference the 
same table twice, and the join condition itself is ambiguous, I'm having to 
do something like this:

FooCounty = orm.aliased(County, "FooCounty")
FooRegion = orm.aliased(Region, "BarRegion")
BarCounty = orm.aliased(County, "BarCounty")
BarRegion = orm.aliased(Region, "BarRegion")

session.query(Foo).join(FooBar).join(Bar).join(FooCounty, Foo.county_id == 
FooCounty.id).join(BarCounty, Bar.county_id == BarCounty.id)   # etc. 

Is there a way I can set something up so that FooCounty and BarCounty are 
essentially permanent aliases of County that will each follow the correct 
relationship, and likewise for FooRegion/BarRegion?  In short, I'd like to 
be able to let SQLAlchemy automatically handle the Join condition and such, 
so I can get the above snippet down to a much simpler...

session.query(Foo).join(FooBar).join(Bar).join(FooCounty).join(BarCounty) 
 # etc.

Thanks in advance!

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to