For nesting the joins you have to use the join() construct manually, your 
example is pretty verbose there, here's a shorter example.    Note that 
SQLAlchemy has two "join()" functions - one is sqlalchemy.sql.join, the other 
is sqlalchemy.orm.join.  The main thing you get from orm.join is that you can 
use a relationship as the ON clause as in the example below.


from sqlalchemy import Column, ForeignKey, Integer
from sqlalchemy.orm import Session, relationship, join
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class A(Base):
    __tablename__ = 'a'

    id = Column(Integer, primary_key=True)
    bs = relationship("B")


class B(Base):
    __tablename__ = 'b'

    id = Column(Integer, primary_key=True)
    a_id = Column(Integer, ForeignKey('a.id'))
    cs = relationship("C")


class C(Base):
    __tablename__ = 'c'

    id = Column(Integer, primary_key=True)
    b_id = Column(Integer, ForeignKey('b.id'))

sess = Session()

j1 = join(B, C, B.cs)

q = sess.query(A).join(j1)

print q


On Aug 10, 2014, at 3:13 AM, mike waites <[email protected]> wrote:

> Hey!
> 
> I not sure if this is entirely possible in SQA and i'm more than open to 
> suggestions for ways to improve the query itself but im looking to produce 
> the following.
> 
> https://gist.github.com/mikeywaites/9e7290eaddcf003e4407
> 
> The thing in particular i'm having trouble with is the syntax for the nested 
> joins.  The below doesn't work but i thought it would serve as a start point 
> for anyone able to help.
> 
> https://gist.github.com/mikeywaites/59476c0fd91301b53697
> 
> Some related material i've found:
> 
> https://bitbucket.org/zzzeek/sqlalchemy/issue/2120/support-nesting-of-joins-with-joined-eager
> http://docs.sqlalchemy.org/en/rel_0_9/changelog/migration_09.html#right-nested-inner-joins-available-in-joined-eager-loads
> 
> So mostly its just the syntax of the nested joins im struggling with at the 
> moment.  I can't quite nail the alias + nesting combinations!
> 
> Again, any suggestions on improving the query overall are more than welcome 
> too!!
> 
> Thanks!
> 
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to