Skip to content

Commit

Permalink
Add name parameter to with_polymorphic.
Browse files Browse the repository at this point in the history
Added missing parameter :paramref:`_orm.with_polymorphic.name` that
allows specifying the name of returned :class:`_orm.AliasedClass`.

Fixes: #11361
Change-Id: I1eae550452526d85da1377207c5fa5e93ac673c3
  • Loading branch information
CaselIT committed May 6, 2024
1 parent c6a2806 commit 02001e9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
6 changes: 6 additions & 0 deletions doc/build/changelog/unreleased_20/11361.rst
Original file line number Diff line number Diff line change
@@ -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`.
6 changes: 6 additions & 0 deletions lib/sqlalchemy/orm/_orm_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2323,6 +2323,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
Expand Down Expand Up @@ -2394,6 +2395,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,
Expand All @@ -2404,6 +2409,7 @@ def with_polymorphic(
adapt_on_names=adapt_on_names,
aliased=aliased,
innerjoin=innerjoin,
name=name,
_use_mapper_path=_use_mapper_path,
)

Expand Down
2 changes: 2 additions & 0 deletions lib/sqlalchemy/orm/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,7 @@ def _with_polymorphic_factory(
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)
Expand All @@ -1089,6 +1090,7 @@ def _with_polymorphic_factory(
return AliasedClass(
base,
selectable,
name=name,
with_polymorphic_mappers=mappers,
adapt_on_names=adapt_on_names,
with_polymorphic_discriminator=polymorphic_on,
Expand Down
8 changes: 8 additions & 0 deletions test/orm/inheritance/test_polymorphic_rel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2060,6 +2060,14 @@ def test_correlation_three(self):
[(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):
Expand Down
17 changes: 5 additions & 12 deletions test/orm/test_cache_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,15 +643,9 @@ def test_wpoly_cache_keys(self):
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(
Expand Down Expand Up @@ -695,9 +689,7 @@ def test_wpoly_cache_keys(self):
aliased=True,
),
),
inspect(
with_polymorphic(Person, [Manager, Engineer, Boss]),
),
inspect(with_polymorphic(Person, [Manager, Engineer, Boss])),
inspect(
with_polymorphic(
Person,
Expand All @@ -712,6 +704,7 @@ def test_wpoly_cache_keys(self):
polymorphic_on=literal_column("bar"),
),
),
inspect(with_polymorphic(Person, "*", name="foo")),
),
compare_values=True,
)
Expand Down

0 comments on commit 02001e9

Please sign in to comment.