On Mar 5, 2008, at 4:14 AM, Christoph Haas wrote:
> Meanwhile I re-read > http://www.sqlalchemy.org/docs/04/ormtutorial.html#datamapping_joins > explaining that a relation path A->bars->B->bats->C->widgets->D is > queried as: > > session.query(Foo).join(['bars', 'bats', 'widgets']).filter(...) > > So in my example I already managed to get this working: > > session.query(Employee)join(['department','company']). \ > filter(model.Company.id==mycompany.id) > > Is this the common way to deal with Company>Department->Employee > paths? > If it is - can I perhaps even omit the ".id" part somehow? I tried: > > session.query(Employee)join(['department','company']). \ > filter(model.Company==mycompany) > > But SQLAlchemy didn't like that: > > ArgumentError: filter() argument must be of type > sqlalchemy.sql.ClauseElement or string > you want to compare Employee.department.company to the element you have, so it would be: session.query(Employee).join('department').filter(Department.company == mycompany) or even cooler, you could do session .query (Employee ).filter(Employee.department.has(Department.company==mycompany)) both require fewer joins than what you're doing...since department- >company is many to one, the "companies" table doesn't actually need to be in the SQL, just department.company_id==<mycompany.id>. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---
