session.autoflush = autoflush
-def get_from_identity(session, key, passive):
+def get_from_identity(session, mapper, key, passive):
"""Look up the given key in the given session's identity map,
check the object for expired state if found.
state = attributes.instance_state(instance)
+ if mapper.inherits and not state.mapper.isa(mapper):
+ return attributes.PASSIVE_CLASS_MISMATCH
+
# expired - ensure it still exists
if state.expired:
if not passive & attributes.SQL_OK:
key = mapper.identity_key_from_primary_key(
primary_key_identity, identity_token=identity_token
)
- return loading.get_from_identity(self.session, key, passive)
+ return loading.get_from_identity(self.session, mapper, key, passive)
def _get_impl(self, primary_key_identity, db_load_fn, identity_token=None):
# convert composite types to individual args
if not issubclass(instance.__class__, mapper.class_):
return None
return instance
+ elif instance is attributes.PASSIVE_CLASS_MISMATCH:
+ return None
return db_load_fn(self, primary_key_identity)
from sqlalchemy.testing import eq_
from sqlalchemy.testing import fixtures
from sqlalchemy.testing import is_
+from sqlalchemy.testing.entities import ComparableEntity
from sqlalchemy.testing.schema import Column
from sqlalchemy.testing.schema import Table
"seen AS seen_1 ON people.id = seen_1.id LEFT OUTER JOIN "
"seen AS seen_2 ON people_1.id = seen_2.id",
)
+
+
+class M2ODontLoadSiblingTest(fixtures.DeclarativeMappedTest):
+ """test for #5210"""
+
+ @classmethod
+ def setup_classes(cls):
+ Base = cls.DeclarativeBasic
+
+ class Parent(Base, ComparableEntity):
+ __tablename__ = "parents"
+
+ id = Column(Integer, primary_key=True)
+ child_type = Column(String(50), nullable=False)
+
+ __mapper_args__ = {
+ "polymorphic_on": child_type,
+ }
+
+ class Child1(Parent):
+ __tablename__ = "children_1"
+
+ id = Column(Integer, ForeignKey(Parent.id), primary_key=True)
+
+ __mapper_args__ = {
+ "polymorphic_identity": "child1",
+ }
+
+ class Child2(Parent):
+ __tablename__ = "children_2"
+
+ id = Column(Integer, ForeignKey(Parent.id), primary_key=True)
+
+ __mapper_args__ = {
+ "polymorphic_identity": "child2",
+ }
+
+ class Other(Base):
+ __tablename__ = "others"
+
+ id = Column(Integer, primary_key=True)
+ parent_id = Column(Integer, ForeignKey(Parent.id))
+
+ parent = relationship(Parent)
+ child2 = relationship(Child2, viewonly=True)
+
+ @classmethod
+ def insert_data(cls):
+ Other, Child1 = cls.classes("Other", "Child1")
+ s = Session()
+ obj = Other(parent=Child1())
+ s.add(obj)
+ s.commit()
+
+ def test_load_m2o_emit_query(self):
+ Other, Child1 = self.classes("Other", "Child1")
+ s = Session()
+
+ obj = s.query(Other).first()
+
+ is_(obj.child2, None)
+ eq_(obj.parent, Child1())
+
+ def test_load_m2o_use_get(self):
+ Other, Child1 = self.classes("Other", "Child1")
+ s = Session()
+
+ obj = s.query(Other).first()
+ c1 = s.query(Child1).first()
+
+ is_(obj.child2, None)
+ is_(obj.parent, c1)