]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Pass through plain selectable to .alias()
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 7 Dec 2020 15:08:06 +0000 (10:08 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 7 Dec 2020 15:08:06 +0000 (10:08 -0500)
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

doc/build/changelog/unreleased_14/5750.rst [new file with mode: 0644]
lib/sqlalchemy/orm/util.py
test/orm/test_utils.py

diff --git a/doc/build/changelog/unreleased_14/5750.rst b/doc/build/changelog/unreleased_14/5750.rst
new file mode 100644 (file)
index 0000000..fde0d5d
--- /dev/null
@@ -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.
+
index 4502d5b8919edc5e58d1113c3d1b50944bba34a0..886ae9a116ba950e9be334916916eb72d2fa6b2d 100644 (file)
@@ -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,
index 1d98826782165396dcf62010b605997804461c6d..d3082accdaf8b3459a1ed2154f3e52070e651694 100644 (file)
@@ -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