subquery eager loading doesn't use join_depth correctly

Issue #3967 resolved
Mike Bayer repo owner created an issue

the join_depth parameter w/ subquery eager loading causes it to just load infinitely as long as there are objects

diff --git a/test/orm/test_subquery_relations.py b/test/orm/test_subquery_relations.py
index 1396281..2b8068d 100644
--- a/test/orm/test_subquery_relations.py
+++ b/test/orm/test_subquery_relations.py
@@ -517,6 +517,29 @@ class EagerTest(_fixtures.FixtureTest, testing.AssertsCompiledSQL):
         eq_(self.static.user_address_result,
             sess.query(User).order_by(User.id).all())

+    def test_cyclical_explicit_join_depth(self):
+        """A circular eager relationship breaks the cycle with a lazy loader"""
+
+        Address, addresses, users, User = (self.classes.Address,
+                                           self.tables.addresses,
+                                           self.tables.users,
+                                           self.classes.User)
+
+        mapper(Address, addresses)
+        mapper(User, users, properties=dict(
+            addresses=relationship(Address, lazy='subquery', join_depth=1,
+                                   backref=sa.orm.backref(
+                                       'user', lazy='subquery', join_depth=1),
+                                   order_by=Address.id)
+        ))
+        is_(sa.orm.class_mapper(User).get_property('addresses').lazy,
+            'subquery')
+        is_(sa.orm.class_mapper(Address).get_property('user').lazy, 'subquery')
+
+        sess = create_session()
+        eq_(self.static.user_address_result,
+            sess.query(User).order_by(User.id).all())
+
     def test_double(self):
         """Eager loading with two relationships simultaneously,
             from the same table, using aliases."""

will cause a crash.

similarly, the test "test_lazy_fallback_doesnt_affect_eager" doesn't make sense; the subquery loads are emitted for all levels deep when the join_depth=1. it should only eagerly load one level deep.

Comments (1)

  1. Mike Bayer reporter

    Accommodate for query._current_path in subq eager load join_depth

    Fixed bug in subquery eager loading where the "join_depth" parameter for self-referential relationships would not be correctly honored, loading all available levels deep rather than correctly counting the specified number of levels for eager loading.

    Change-Id: Ifa54085cbab3b41c2196f3ee519f485c63e4cb8d Fixes: #3967

    → <<cset 212132958f6c>>

  2. Log in to comment