From: Mike Bayer Date: Mon, 7 Dec 2020 15:08:06 +0000 (-0500) Subject: Pass through plain selectable to .alias() X-Git-Tag: rel_1_4_0b2~118 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1dc0b34b959d3571f8b21425bb43edb9c87d4a19;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Pass through plain selectable to .alias() Fixed regression where creating an :class:`_orm.aliased` construct against a plain selectable and including a name would raise an assertionerror. Fixes: #5750 Change-Id: I98fbc7c7bd2c88f0600fc507275eb8b1bb76d8df --- diff --git a/doc/build/changelog/unreleased_14/5750.rst b/doc/build/changelog/unreleased_14/5750.rst new file mode 100644 index 0000000000..fde0d5d98c --- /dev/null +++ b/doc/build/changelog/unreleased_14/5750.rst @@ -0,0 +1,7 @@ +.. change:: + :tags: bug, orm + :tickets: 5750 + + Fixed regression where creating an :class:`_orm.aliased` construct against + a plain selectable and including a name would raise an assertionerror. + diff --git a/lib/sqlalchemy/orm/util.py b/lib/sqlalchemy/orm/util.py index 4502d5b891..886ae9a116 100644 --- a/lib/sqlalchemy/orm/util.py +++ b/lib/sqlalchemy/orm/util.py @@ -1134,9 +1134,12 @@ def aliased(element, alias=None, name=None, flat=False, adapt_on_names=False): raise sa_exc.ArgumentError( "adapt_on_names only applies to ORM elements" ) - return coercions.expect( - roles.AnonymizedFromClauseRole, element, name=name, flat=flat - ) + if name: + return element.alias(name=name, flat=flat) + else: + return coercions.expect( + roles.AnonymizedFromClauseRole, element, flat=flat + ) else: return AliasedClass( element, diff --git a/test/orm/test_utils.py b/test/orm/test_utils.py index 1d98826782..d3082accda 100644 --- a/test/orm/test_utils.py +++ b/test/orm/test_utils.py @@ -2,6 +2,7 @@ from sqlalchemy import Column from sqlalchemy import inspect from sqlalchemy import Integer from sqlalchemy import MetaData +from sqlalchemy import select from sqlalchemy import Table from sqlalchemy import util from sqlalchemy.ext.hybrid import hybrid_method @@ -55,6 +56,30 @@ class AliasedClassTest(fixtures.TestBase, AssertsCompiledSQL): assert Point.id.__clause_element__().table is table assert alias.id.__clause_element__().table is not table + def test_named_entity(self): + class Point(object): + pass + + self._fixture(Point) + + alias = aliased(Point, name="pp") + + self.assert_compile( + select(alias), "SELECT pp.id, pp.x, pp.y FROM point AS pp" + ) + + def test_named_selectable(self): + class Point(object): + pass + + table = self._fixture(Point) + + alias = aliased(table, name="pp") + + self.assert_compile( + select(alias), "SELECT pp.id, pp.x, pp.y FROM point AS pp" + ) + def test_not_instantiatable(self): class Point(object): pass