From: Federico Caselli Date: Mon, 6 May 2024 21:10:46 +0000 (+0200) Subject: Add name parameter to with_polymorphic. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=02001e9458802ebb512a140aa24e663b364dc3ad;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Add name parameter to with_polymorphic. Added missing parameter :paramref:`_orm.with_polymorphic.name` that allows specifying the name of returned :class:`_orm.AliasedClass`. Fixes: #11361 Change-Id: I1eae550452526d85da1377207c5fa5e93ac673c3 --- diff --git a/doc/build/changelog/unreleased_20/11361.rst b/doc/build/changelog/unreleased_20/11361.rst new file mode 100644 index 0000000000..bd9fe1d3ff --- /dev/null +++ b/doc/build/changelog/unreleased_20/11361.rst @@ -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`. diff --git a/lib/sqlalchemy/orm/_orm_constructors.py b/lib/sqlalchemy/orm/_orm_constructors.py index 2639db2897..0bb6e31919 100644 --- a/lib/sqlalchemy/orm/_orm_constructors.py +++ b/lib/sqlalchemy/orm/_orm_constructors.py @@ -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 @@ -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, @@ -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, ) diff --git a/lib/sqlalchemy/orm/util.py b/lib/sqlalchemy/orm/util.py index 81b6eb23a8..d1dbf22639 100644 --- a/lib/sqlalchemy/orm/util.py +++ b/lib/sqlalchemy/orm/util.py @@ -1069,6 +1069,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) @@ -1089,6 +1090,7 @@ class AliasedInsp( return AliasedClass( base, selectable, + name=name, with_polymorphic_mappers=mappers, adapt_on_names=adapt_on_names, with_polymorphic_discriminator=polymorphic_on, diff --git a/test/orm/inheritance/test_polymorphic_rel.py b/test/orm/inheritance/test_polymorphic_rel.py index 0b358f8894..1216aa0106 100644 --- a/test/orm/inheritance/test_polymorphic_rel.py +++ b/test/orm/inheritance/test_polymorphic_rel.py @@ -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): diff --git a/test/orm/test_cache_key.py b/test/orm/test_cache_key.py index ff70e4718b..4bd353b84f 100644 --- a/test/orm/test_cache_key.py +++ b/test/orm/test_cache_key.py @@ -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, )