Relaciones SQLAlchemy a través de múltiples tablas

Estoy tratando de crear una relación que se extiende a lo largo de 3 tablas, pero no puedo entender la syntax.

Tengo 3 tablas TableA , TableB y TableC y la relación que estoy tratando de modelar es:

 TableA.my_relationship = relationship( 'TableC', primaryjoin='and_( ==, ==', viewonly=True ) 

de modo que en una instancia de TableA puedo hacer instance_of_a.my_relationship para obtener el registro de TableC que está asociado indirectamente con instance_of_a

 from sqlalchemy import * from sqlalchemy.orm import * from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class A(Base): __tablename__ = 'a' id = Column(Integer, primary_key=True) b_id = Column(Integer, ForeignKey('')) # method one - put everything into primaryjoin. # will work for simple lazy loads but for eager loads the ORM # will fail to build up the FROM to correctly include B cs = relationship("C", # is "foreign" because there can be many C.ids for one # is "remote", it sort of means "this is where the stuff # starts that's not directly part of the A side" primaryjoin="and_(A.b_id == remote(, foreign( == B.c_id)", viewonly=True) # method two - split out the middle table into "secondary". # note 'b' is the table name in metadata. # this method will work better, as the ORM can also handle # eager loading with this one. c_via_secondary = relationship("C", secondary="b", primaryjoin="A.b_id ==", secondaryjoin=" == B.c_id", viewonly=True) class B(Base): __tablename__ = 'b' id = Column(Integer, primary_key=True) c_id = Column(Integer, ForeignKey('')) class C(Base): __tablename__ = 'c' id = Column(Integer, primary_key=True) e = create_engine("sqlite://", echo=True) Base.metadata.create_all(e) sess = Session(e) sess.add(C(id=1)) sess.flush() sess.add(B(id=1, c_id=1)) sess.flush() sess.add(A(b_id=1)) sess.flush() a1 = sess.query(A).first() print(a1.cs) print(a1.c_via_secondary)