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


-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/474ccb9b-6839-47b7-9d38-fd1a7065f7a4%40googlegroups.com.

Reply via email to