]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Add name parameter to with_polymorphic.
authorFederico Caselli <cfederico87@gmail.com>
Mon, 6 May 2024 21:10:46 +0000 (23:10 +0200)
committerFederico Caselli <cfederico87@gmail.com>
Mon, 6 May 2024 21:11:04 +0000 (23:11 +0200)
Added missing parameter :paramref:`_orm.with_polymorphic.name` that
allows specifying the name of returned :class:`_orm.AliasedClass`.

Fixes: #11361
Change-Id: I1eae550452526d85da1377207c5fa5e93ac673c3
(cherry picked from commit 02001e9458802ebb512a140aa24e663b364dc3ad)

doc/build/changelog/unreleased_20/11361.rst [new file with mode: 0644]
lib/sqlalchemy/orm/_orm_constructors.py
lib/sqlalchemy/orm/util.py
test/orm/inheritance/test_polymorphic_rel.py
test/orm/test_cache_key.py

diff --git a/doc/build/changelog/unreleased_20/11361.rst b/doc/build/changelog/unreleased_20/11361.rst
new file mode 100644 (file)
index 0000000..bd9fe1d
--- /dev/null
@@ -0,0 +1,6 @@
+.. change::
+    :tags: usecase, orm
+    :tickets: 11361
+
+    Added missing parameter :paramref:`_orm.with_polymorphic.name` that
+    allows specifying the name of returned :class:`_orm.AliasedClass`.
index 7cb536b29766da11bb9527d1bded81745702805c..3403c39e29fc8d5d0cfb6358d8d5589154923d77 100644 (file)
@@ -2317,6 +2317,7 @@ def with_polymorphic(
     aliased: bool = False,
     innerjoin: bool = False,
     adapt_on_names: bool = False,
+    name: Optional[str] = None,
     _use_mapper_path: bool = False,
 ) -> AliasedClass[_O]:
     """Produce an :class:`.AliasedClass` construct which specifies
@@ -2388,6 +2389,10 @@ def with_polymorphic(
 
       .. versionadded:: 1.4.33
 
+    :param name: Name given to the generated :class:`.AliasedClass`.
+
+      .. versionadded:: 2.0.31
+
     """
     return AliasedInsp._with_polymorphic_factory(
         base,
@@ -2398,6 +2403,7 @@ def with_polymorphic(
         adapt_on_names=adapt_on_names,
         aliased=aliased,
         innerjoin=innerjoin,
+        name=name,
         _use_mapper_path=_use_mapper_path,
     )
 
index 8e153e63dbdb2b5ad136d3b0e30002b8b24c62f0..ad2b69ce3135bf0e7fe405e944e3dbe053ccb721 100644 (file)
@@ -1067,6 +1067,7 @@ class AliasedInsp(
         aliased: bool = False,
         innerjoin: bool = False,
         adapt_on_names: bool = False,
+        name: Optional[str] = None,
         _use_mapper_path: bool = False,
     ) -> AliasedClass[_O]:
         primary_mapper = _class_to_mapper(base)
@@ -1087,6 +1088,7 @@ class AliasedInsp(
         return AliasedClass(
             base,
             selectable,
+            name=name,
             with_polymorphic_mappers=mappers,
             adapt_on_names=adapt_on_names,
             with_polymorphic_discriminator=polymorphic_on,
index 0b358f8894b31c264795a664fae2191168a73500..1216aa0106f81e4b7fc98f5a39f87c95c90161a1 100644 (file)
@@ -2060,6 +2060,14 @@ class _PolymorphicTestBase:
             [(e3.name,)],
         )
 
+    def test_with_polymorphic_named(self):
+        session = fixture_session()
+        poly = with_polymorphic(Person, "*", name="poly_name")
+
+        res = session.execute(select(poly)).mappings()
+        eq_(res.keys(), ["poly_name"])
+        eq_(len(res.all()), 5)
+
 
 class PolymorphicTest(_PolymorphicTestBase, _Polymorphic):
     def test_joined_aliasing_unrelated_subuqery(self):
index ff70e4718b52dc0020405ebe2045a2b252b24c46..4bd353b84fd139e64f54870abd89875ec51ee1cb 100644 (file)
@@ -643,15 +643,9 @@ class PolyCacheKeyTest(fixtures.CacheKeyFixture, _poly_fixtures._Polymorphic):
         self._run_cache_key_fixture(
             lambda: (
                 inspect(Person),
-                inspect(
-                    aliased(Person, me_stmt),
-                ),
-                inspect(
-                    aliased(Person, meb_stmt),
-                ),
-                inspect(
-                    with_polymorphic(Person, [Manager, Engineer]),
-                ),
+                inspect(aliased(Person, me_stmt)),
+                inspect(aliased(Person, meb_stmt)),
+                inspect(with_polymorphic(Person, [Manager, Engineer])),
                 # aliased=True is the same as flat=True for default selectable
                 inspect(
                     with_polymorphic(
@@ -695,9 +689,7 @@ class PolyCacheKeyTest(fixtures.CacheKeyFixture, _poly_fixtures._Polymorphic):
                         aliased=True,
                     ),
                 ),
-                inspect(
-                    with_polymorphic(Person, [Manager, Engineer, Boss]),
-                ),
+                inspect(with_polymorphic(Person, [Manager, Engineer, Boss])),
                 inspect(
                     with_polymorphic(
                         Person,
@@ -712,6 +704,7 @@ class PolyCacheKeyTest(fixtures.CacheKeyFixture, _poly_fixtures._Polymorphic):
                         polymorphic_on=literal_column("bar"),
                     ),
                 ),
+                inspect(with_polymorphic(Person, "*", name="foo")),
             ),
             compare_values=True,
         )