i'm stuck on a variant of the Composite Secondary Join (
https://docs.sqlalchemy.org/en/13/orm/join_conditions.html#composite-secondary-joins)
I hope someone can see what my tired eyes are missing. I'm fairly certain the issue is in `secondary' and 'secondaryjoin'. I've tried a handful of variants I thought would work, but trigger this error:
sqlalchemy.exc.InvalidRequestError: Class <class '__main__.C'> does not have a mapped column named 'get_children'
I've used this pattern with much luck in the past: `primaryjoin` goes from the base class to whatever I build the secondary/secondary join from.
I've distilled the relationship pattern as below:
* `A` does not fkey onto anything.
* `B` fkeys onto `A`
* The intended relationship on `A` is a list of `C` items, which are related to `B` through an association table
I can build out a the relationship from B, and I could probably mount it onto A with an association_proxy, but I'd like to understand what I'm doing wrong with the `relationship` off A. This is puzzling me.
Thanks in advace.
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Integer
from sqlalchemy import Column
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Base = declarative_base()
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
class A(Base):
__tablename__ = 'a'
id = Column(Integer, primary_key=True)
cs = relationship(
"C",
primaryjoin="A.id == B.a_id",
secondary="join(B, B2C, B.id == B2C.b_id)."
"join(B2C, C, B2C.c_id == C.id)",
# secondaryjoin="and_(C.id == B2C.c_id)",
viewonly=True
)
class B(Base):
__tablename__ = 'b'
id = Column(Integer, primary_key=True)
a_id = Column(ForeignKey('a.id'))
class B2C(Base):
__tablename__ = 'b2c'
id = Column(Integer, primary_key=True)
b_id = Column(ForeignKey('b.id'))
c_id = Column(ForeignKey('c.id'))
class C(Base):
__tablename__ = 'c'
id = Column(Integer, primary_key=True)
engine = create_engine("sqlite://", echo=True)
Base.metadata.create_all(engine)
sessionFactory = sessionmaker(bind=engine)
sess = sessionFactory()
sess.query(A).join(a.cs).all()