Skip to content

keyerror in subqueryload w complex inheritance #2617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sqlalchemy-bot opened this issue Nov 26, 2012 · 6 comments
Closed

keyerror in subqueryload w complex inheritance #2617

sqlalchemy-bot opened this issue Nov 26, 2012 · 6 comments
Labels
blocker issue that must be resolved asap as it is preventing things from working bug Something isn't working orm
Milestone

Comments

@sqlalchemy-bot
Copy link
Collaborator

Migrated issue, originally created by Michael Bayer (@zzzeek)

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import *
from sqlalchemy.orm import relationship, subqueryload, Session

Base = declarative_base()

class Z(Base):
    __tablename__ = 'z'
    id = Column(Integer, primary_key=True)

class A(Base):
    __tablename__ = 'a'
    id = Column(Integer, primary_key=True)
    type = Column(String)

    ztoa = Table('ztoa', Base.metadata,
        Column('zid', Integer, ForeignKey('z.id'), nullable=False),
        Column('aid', Integer, ForeignKey('a.id'), nullable=False)
    )
    zs = relationship("Z", secondary=ztoa, lazy="subquery", backref="as_")
    __mapper_args__ = {'polymorphic_on': type, 'with_polymorphic':'*'}

class B(A):
    __tablename__ = 'b'
    id = Column(Integer, ForeignKey('a.id'), primary_key=True)

    btod = Table('btod', Base.metadata,
        Column('bid', Integer, ForeignKey('b.id'), nullable=False),
        Column('did', Integer, ForeignKey('d.id'), nullable=False)
    )
    related = relationship("D", secondary=btod, lazy="subquery")
    __mapper_args__ = {'polymorphic_identity': 'b'}

class D(A):
    __tablename__ = 'd'
    id = Column(Integer, ForeignKey('a.id'), primary_key=True)
    __mapper_args__ = {'polymorphic_identity': 'd'}

engine = create_engine('sqlite://', echo=True)
Base.metadata.create_all(engine)
session = Session(engine)

z = Z()
d = D()
session.add_all([   Z(as_=[B(related=[d](
))])
])
session.commit()

z = session.query(Z).one()
print z.as_
@sqlalchemy-bot
Copy link
Collaborator Author

Michael Bayer (@zzzeek) wrote:

fortunately (sort of) the #2614 branch also repairs this issue, since this is a search for "related" on the wrong parent class. Though the issue is also a regression vs. 0.7, where this somehow manages to not have this problem.

@sqlalchemy-bot
Copy link
Collaborator Author

Michael Bayer (@zzzeek) wrote:

great, here's why its a regression. because of #2481. here's how to reproduce in 0.7:

diff -r f309abb638db27325f5614c797ff951d1b3fc638 lib/sqlalchemy/orm/strategies.py
--- a/lib/sqlalchemy/orm/strategies.py	Sun Nov 18 12:36:24 2012 -0500
+++ b/lib/sqlalchemy/orm/strategies.py	Wed Nov 28 19:29:34 2012 -0500
@@ -730,10 +730,12 @@
                 if len(path) / 2 > self.join_depth:
                     return
             else:
-                if self.mapper.base_mapper in \
-                    interfaces._reduce_path(subq_path):
+                if self.mapper in subq_path:
+                #if self.mapper.base_mapper in \
+                #    interfaces._reduce_path(subq_path):
                     return
 
+
         subq_mapper, leftmost_mapper, leftmost_attr = \
                 self._get_leftmost(subq_path)

@sqlalchemy-bot
Copy link
Collaborator Author

Michael Bayer (@zzzeek) wrote:

so that makes more sense as to why #2614 is needed here.

@sqlalchemy-bot
Copy link
Collaborator Author

Michael Bayer (@zzzeek) wrote:

here's a simpler version:

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import *
from sqlalchemy.orm import relationship, subqueryload, Session

Base = declarative_base()

class Z(Base):
    __tablename__ = 'z'
    id = Column(Integer, primary_key=True)

class A(Base):
    __tablename__ = 'a'
    id = Column(Integer, primary_key=True)
    type = Column(String)
    z_id = Column(Integer, ForeignKey('z.id'))
    zs = relationship("Z", lazy="subquery")
    __mapper_args__ = {'polymorphic_on': type, 'with_polymorphic': '*'}

class B(A):
    __tablename__ = 'b'
    id = Column(Integer, ForeignKey('a.id'), primary_key=True)

    related = relationship("D", lazy="subquery", primaryjoin="D.b_id==B.id")
    __mapper_args__ = {'polymorphic_identity': 'b'}

class D(A):
    __tablename__ = 'd'
    id = Column(Integer, ForeignKey('a.id'), primary_key=True)
    b_id = Column(Integer, ForeignKey('b.id'))
    __mapper_args__ = {'polymorphic_identity': 'd'}

engine = create_engine('sqlite://', echo=True)
Base.metadata.create_all(engine)
session = Session(engine)

session.add_all([   B()
](
))
session.commit()

a = session.query(A).first()

@sqlalchemy-bot
Copy link
Collaborator Author

Michael Bayer (@zzzeek) wrote:

b66dad4

@sqlalchemy-bot
Copy link
Collaborator Author

Changes by Michael Bayer (@zzzeek):

  • changed status to closed

@sqlalchemy-bot sqlalchemy-bot added bug Something isn't working blocker issue that must be resolved asap as it is preventing things from working orm labels Nov 27, 2018
@sqlalchemy-bot sqlalchemy-bot added this to the 0.8.0final milestone Nov 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocker issue that must be resolved asap as it is preventing things from working bug Something isn't working orm
Projects
None yet
Development

No branches or pull requests

1 participant