]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Use exprs for bundle __clause_element__
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 13 Jul 2018 16:58:21 +0000 (12:58 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 13 Jul 2018 22:56:22 +0000 (18:56 -0400)
Fixed bug in :class:`.Bundle` construct where placing two columns of the
same name would be de-duplicated, when the :class:`.Bundle` were used as
part of the rendered SQL, such as in the ORDER BY or GROUP BY of the statement.

Change-Id: Ia528c9fbb399a6beb5ea7cdd3a8a83ad530f5831
Fixes: #4295
doc/build/changelog/unreleased_12/4295.rst [new file with mode: 0644]
lib/sqlalchemy/orm/query.py
test/orm/test_bundle.py

diff --git a/doc/build/changelog/unreleased_12/4295.rst b/doc/build/changelog/unreleased_12/4295.rst
new file mode 100644 (file)
index 0000000..acfc8a0
--- /dev/null
@@ -0,0 +1,8 @@
+.. change::
+    :tags: bug, orm
+    :tickets: 4295
+
+    Fixed bug in :class:`.Bundle` construct where placing two columns of the
+    same name would be de-duplicated, when the :class:`.Bundle` were used as
+    part of the rendered SQL, such as in the ORDER BY or GROUP BY of the statement.
+
index 98747c680147456013e49bc0a23ba93f4b157518..272fed3e23546b83b33405f4e5d65950973ce2a2 100644 (file)
@@ -3964,7 +3964,7 @@ class Bundle(InspectionAttr):
         return cloned
 
     def __clause_element__(self):
-        return expression.ClauseList(group=False, *self.c)
+        return expression.ClauseList(group=False, *self.exprs)
 
     @property
     def clauses(self):
index 42a29d13ce33253e524ba128cc4d422b3d8c1ba1..cf1eb40c981582c573482f8625a15fd98438dbdf 100644 (file)
@@ -4,6 +4,7 @@ from sqlalchemy.orm import Bundle, Session
 from sqlalchemy.testing import AssertsCompiledSQL
 from sqlalchemy import Integer, select, ForeignKey, String, func
 from sqlalchemy.orm import mapper, relationship, aliased
+from sqlalchemy.sql.elements import ClauseList
 
 
 class BundleTest(fixtures.MappedTest, AssertsCompiledSQL):
@@ -54,6 +55,44 @@ class BundleTest(fixtures.MappedTest, AssertsCompiledSQL):
         ])
         sess.commit()
 
+    def test_same_named_col_clauselist(self):
+        Data, Other = self.classes("Data", "Other")
+        bundle = Bundle("pk", Data.id, Other.id)
+
+        self.assert_compile(
+            ClauseList(Data.id, Other.id),
+            "data.id, other.id"
+        )
+        self.assert_compile(
+            bundle.__clause_element__(),
+            "data.id, other.id"
+        )
+
+    def test_same_named_col_in_orderby(self):
+        Data, Other = self.classes("Data", "Other")
+        bundle = Bundle("pk", Data.id, Other.id)
+        sess = Session()
+
+        self.assert_compile(
+            sess.query(Data, Other).order_by(bundle),
+            "SELECT data.id AS data_id, data.d1 AS data_d1, "
+            "data.d2 AS data_d2, data.d3 AS data_d3, "
+            "other.id AS other_id, other.data_id AS other_data_id, "
+            "other.o1 AS other_o1 "
+            "FROM data, other ORDER BY data.id, other.id"
+        )
+
+    def test_same_named_col_in_fetch(self):
+        Data, Other = self.classes("Data", "Other")
+        bundle = Bundle("pk", Data.id, Other.id)
+        sess = Session()
+
+        eq_(
+            sess.query(bundle).filter(
+                Data.id == Other.id).filter(Data.id < 3).all(),
+            [((1, 1),), ((2, 2),)]
+        )
+
     def test_c_attr(self):
         Data = self.classes.Data