> -----Original Message-----
> From: [email protected] [mailto:[email protected]]
> On Behalf Of Jules Stevenson
> Sent: 09 June 2011 08:53
> To: [email protected]
> Subject: [sqlalchemy] Re: Trying to query a relationship of a
> relationship
> 
> Sorry, for the spamming, code typo (was trying to simplify it),
> should read:
> 
>       invoices = query(ArkInvoice).\
>                 join(ArkInvoice.project).\
>                 join(ArkProject.client).\
> 
> options(sa.orm.contains_eager(ArkInvoice.project.client)).\
>                 filter(ArkInvoice.project.client.id == id)
> 

I think you probably want something like this (all untested):

invoices = (session.query(ArkInvoice)
            .join(ArkInvoice.project)
            .join(ArkProject.client)
            .filter(ArkClient.id == id)).all()

If you need contains_eager (which is purely an optimisation allowing you
to access invoice.project without a subsequent query), I think it would
look like this:

invoices = (session.query(ArkInvoice)
            .join(ArkInvoice.project)
            .join(ArkProject.client)
            .options(contains_eager(ArkInvoice.project),
                     contains_eager(ArkProject.client))
            .filter(ArkClient.id == id)
            .all())


However, if you are actually going to be working with the client,
project and invoice objects after this query, you may find it easier to
start from the client:

client = (session.query(ArkClient)
          .options(joinedload_all('projects.invoices'))
          .filter(ArkClient.id == id)
          .one())

After this query, you could access client.projects and
client.projects[n].invoices without further database queries.

See http://www.sqlalchemy.org/docs/orm/loading.html for a description of
joinedload_all.

I hope that helps,

Simon

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