From: Mike Bayer Date: Fri, 13 Jul 2018 16:58:21 +0000 (-0400) Subject: Use exprs for bundle __clause_element__ X-Git-Tag: rel_1_3_0b1~126 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2fdf26020878edcbaa7792a869b3d45b715cc05a;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Use exprs for bundle __clause_element__ 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 --- diff --git a/doc/build/changelog/unreleased_12/4295.rst b/doc/build/changelog/unreleased_12/4295.rst new file mode 100644 index 0000000000..acfc8a0746 --- /dev/null +++ b/doc/build/changelog/unreleased_12/4295.rst @@ -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. + diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index 98747c6801..272fed3e23 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -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): diff --git a/test/orm/test_bundle.py b/test/orm/test_bundle.py index 42a29d13ce..cf1eb40c98 100644 --- a/test/orm/test_bundle.py +++ b/test/orm/test_bundle.py @@ -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