Moin, Michael...

thanks for your quick reply.

On Wed, Mar 05, 2008 at 11:16:33AM -0500, Michael Bayer wrote:
> 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)

Oh, right. That saves the second JOIN as the department.company_id is
already a field on the departments. Yet another case of thinking too
abstract instead of remembering how the actual database tables look. :)

> or even cooler, you could do
>        
> session.query(Employee).filter(Employee.department.has(
>   Department.company==mycompany))

Somehow .has and .any have always been somewhat magical to me. I think
I'll rather use the less cool way because it's more likely I understand
my code then in a few years. Works well, thanks. So I was basically on
the right track with the JOIN.

But now I'm curious. Why do I get the ArgumentError if I try

    .filter(Company==my_company)

while

    .filter(Company.id==my_company.id)

works? I was comparing ORM objects directly instead of fields/properties
of a mapped object. But shouldn't that work, too?

Cheers
 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to