]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
warn / document for Query.with_polymorphic() with with_loader_criteria()
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 23 Mar 2021 14:23:23 +0000 (10:23 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 23 Mar 2021 14:23:23 +0000 (10:23 -0400)
These are illustrated as not working in #6111.  As this is
a highly complex and legacy method, encourage users to
migrate off of it before using with_loader_criteria().

Fixes: #6111
Change-Id: I63c8187020c631d83259ea2200b66eabf74a0d0d

lib/sqlalchemy/orm/query.py
lib/sqlalchemy/orm/util.py
test/orm/test_deprecations.py

index 96b4c562443d2442daf135b4d9cb8a3d3e7b8973..7e2fde74972c4d30d9023fff904d2d8e0e8d718c 100644 (file)
@@ -786,6 +786,14 @@ class Query(
     ):
         """Load columns for inheriting classes.
 
+        This is a legacy method which is replaced by the
+        :func:`_orm.with_polymorphic` function.
+
+        .. warning:: The :meth:`_orm.Query.with_polymorphic` method does
+           **not** support 1.4/2.0 style features including
+           :func:`_orm.with_loader_criteria`.  Please migrate code
+           to use :func:`_orm.with_polymorphic`.
+
         :meth:`_query.Query.with_polymorphic` applies transformations
         to the "main" mapped class represented by this :class:`_query.Query`.
         The "main" mapped class here means the :class:`_query.Query`
@@ -796,8 +804,9 @@ class Query(
         purposes of load-time efficiency as well as the ability to use
         these columns at query time.
 
-        See the documentation section :ref:`with_polymorphic` for
-        details on how this method is used.
+        .. seealso::
+
+            :ref:`with_polymorphic` - illustrates current patterns
 
         """
 
index 37be077be7dc671997f0e784530d9f4e1faeaf45..941c5eb0855e302844cd697abfa2e0b17b8ff6c2 100644 (file)
@@ -1070,6 +1070,13 @@ class LoaderCriteriaOption(CriteriaOption):
         # if options to limit the criteria to immediate query only,
         # use compile_state.attributes instead
 
+        if compile_state.compile_options._with_polymorphic_adapt_map:
+            util.warn(
+                "The with_loader_criteria() function may not work "
+                "correctly with the legacy Query.with_polymorphic() feature.  "
+                "Please migrate code to use the with_polymorphic() standalone "
+                "function before using with_loader_criteria()."
+            )
         if not compile_state.compile_options._for_refresh_state:
             self.get_global_criteria(compile_state.global_attributes)
 
index 34e3a483123a7ca672c823763665f6b8155018e7..dd7e79ffd1ab36b21f6892a163c6ecce187f3258 100644 (file)
@@ -42,6 +42,7 @@ from sqlalchemy.orm import Session
 from sqlalchemy.orm import subqueryload
 from sqlalchemy.orm import synonym
 from sqlalchemy.orm import undefer
+from sqlalchemy.orm import with_loader_criteria
 from sqlalchemy.orm import with_parent
 from sqlalchemy.orm import with_polymorphic
 from sqlalchemy.orm.collections import collection
@@ -5056,6 +5057,27 @@ class InheritedJoinTest(_poly_fixtures._Polymorphic, AssertsCompiledSQL):
                 use_default_dialect=True,
             )
 
+    def test_with_poly_loader_criteria_warning(self):
+        Person, Manager = (
+            self.classes.Person,
+            self.classes.Manager,
+        )
+
+        sess = fixture_session()
+
+        with testing.expect_deprecated_20(w_polymorphic_dep):
+            q = (
+                sess.query(Person)
+                .with_polymorphic(Manager)
+                .options(with_loader_criteria(Person, Person.person_id == 1))
+            )
+
+        with testing.expect_warnings(
+            r"The with_loader_criteria\(\) function may not work "
+            r"correctly with the legacy Query.with_polymorphic\(\)"
+        ):
+            str(q)
+
 
 class JoinFromSelectableTest(fixtures.MappedTest, AssertsCompiledSQL):
     __dialect__ = "default"