[Sqlalchemy-tickets] Issue #3256: allow of_type() strategies to be stacked (zzzeek/sqlalchemy)
Brought to you by:
zzzeek
|
From: Mike B. <iss...@bi...> - 2014-11-24 23:17:37
|
New issue 3256: allow of_type() strategies to be stacked https://bitbucket.org/zzzeek/sqlalchemy/issue/3256/allow-of_type-strategies-to-be-stacked Mike Bayer: ``` #!python from sqlalchemy import Integer, ForeignKey, String, Column, create_engine from sqlalchemy.orm import relationship, joinedload, with_polymorphic, Session from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() import logging logging.basicConfig() logging.getLogger("sqlalchemy.orm.path_registry").setLevel(logging.DEBUG) class A(Base): __tablename__ = 'a' id = Column(Integer, primary_key=True) b_id = Column(ForeignKey('b.id')) b = relationship("B") class B(Base): __tablename__ = 'b' id = Column(Integer, primary_key=True) type = Column(String) __mapper_args__ = { 'polymorphic_on': type, } class BSub1(B): __mapper_args__ = { 'polymorphic_identity': 'sub1', } c = relationship("C1", uselist=False) class BSub2(B): __mapper_args__ = { 'polymorphic_identity': 'sub2', } c = relationship("C2", uselist=False) class C1(Base): __tablename__ = 'c1' id = Column(Integer, primary_key=True) b_id = Column(ForeignKey('b.id')) class C2(Base): __tablename__ = 'c2' id = Column(Integer, primary_key=True) b_id = Column(ForeignKey('b.id')) e = create_engine("sqlite://", echo='debug') Base.metadata.create_all(e) s = Session(e) s.add_all([ A(b=BSub1(c=C1())), A(b=BSub2(c=C2())) ]) s.commit() b_poly = with_polymorphic(B, [BSub1, BSub2], aliased=True) q = s.query(A).options( # works # joinedload(A.b.of_type(b_poly)).joinedload(b_poly.BSub1.c), # joinedload(A.b.of_type(b_poly)).joinedload(b_poly.BSub2.c), # fails, eager loads only one of them joinedload(A.b.of_type(BSub1)).joinedload(BSub1.c), joinedload(A.b.of_type(BSub2)).joinedload(BSub2.c), ) for a in q: b = a.b print b.c ``` |