model.c.Company == mycompany?

Just hoping that I didn't mix up the ORMs ;)

Andreas

Am Mittwoch, den 05.03.2008, 10:14 +0100 schrieb Christoph Haas:
> 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

Attachment: signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil

Reply via email to