Thanks so much! I was interpreting the parenthesis as a subquery. Been 
banging my head against the wall thinking this was SQLAlchemy nonsense for 
quite a while and it was just a simple misundertstanding of syntax. Now, 
I've got the information I should be able to fix this within the far 
messier application code. 
On Tuesday, November 3, 2020 at 8:57:18 AM UTC-3:30 Alex Collins wrote:

> Trying to configure a set of relationships to all be joined loaded and the 
> particular relationship structure doesn’t seem to want to join. I have a 
> one-to-many relationship where the many is the child in joined table 
> inheritance. The foreign key to my source table is on the polymorphic child 
> table. But, however I configure the relationship it does a subquery instead 
> of a joined load on the parent class.
>
> Built a test application as a demonstration. What I want is to have the 
> script below function the same way but, the query at the end outputs a 
> joinedload for PolyParent instead of a subquery. 
>
> from sqlalchemy import Column, ForeignKey, Integer, Text, create_engine
> from sqlalchemy.ext.declarative import declarative_base
> from sqlalchemy.orm import relationship, sessionmaker
>
> Base = declarative_base()
>
> class PolyParent(Base):
>     __tablename__ = "poly_parent"
>     id = Column(Integer, primary_key=True)
>     type = Column(Text)
>     __mapper_args__ = {"polymorphic_identity": "poly_parent", 
> "polymorphic_on": type}
>
> class PolyChild(PolyParent):
>     __tablename__ = "poly_child"
>     id = Column(Integer, ForeignKey("poly_parent.id"), primary_key=True)
>     parent_id = Column(Integer, ForeignKey("source.id"))
>     __mapper_args__ = {"polymorphic_identity": "poly_child"}
>
> class Source(Base):
>     __tablename__ = "source"
>     id = Column(Integer, primary_key=True)
>     children = relationship(PolyChild)
>
> engine = create_engine("sqlite://")
> session = sessionmaker(bind=engine)()
> Base.metadata.create_all(bind=engine)
>
> print(session.query(Source).join(Source.children))
>

-- 
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/dad45bfd-a764-4354-8b6c-fe5ce58db925n%40googlegroups.com.

Reply via email to