On Wed, Mar 05, 2008 at 08:31:12AM +0100, Christoph Haas wrote: > Basically I have three tables like 'companies', 'departments' and > 'employees'. I have already set up foreign keys and gave all of them > one-to-many relationships. So a company has several departments. And > each department has several employees. So for an ORM-mapped company > object "mycompany" I can get the departments by the property > "mycompany.departments". Works well. > > Now I'd like to create a query for all employees of a certain company. > And I'm not sure how to properly define a mapper relation propery that > would give me that. Like "mycompany.employees". Do I have to use JOINs > myself in the mapper? > > In my application I'd then like to query like this: > Session.query(Employee).filter_by(employee.company=my_company)
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 Thanks for any comments. Christoph -- [EMAIL PROTECTED] www.workaround.org JID: [EMAIL PROTECTED] gpg key: 79CC6586 fingerprint: 9B26F48E6F2B0A3F7E33E6B7095E77C579CC6586 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
