diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2018-07-13 12:58:21 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2018-07-13 18:56:22 -0400 |
commit | 2fdf26020878edcbaa7792a869b3d45b715cc05a (patch) | |
tree | cf2dcd1e6b7e1aa4461090f3924cfe2239362a4c | |
parent | 2c44fc22a7e9a4ac69ed6ce9da5551eb2d7cc1a2 (diff) | |
download | sqlalchemy-2fdf26020878edcbaa7792a869b3d45b715cc05a.tar.gz |
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
-rw-r--r-- | doc/build/changelog/unreleased_12/4295.rst | 8 | ||||
-rw-r--r-- | lib/sqlalchemy/orm/query.py | 2 | ||||
-rw-r--r-- | test/orm/test_bundle.py | 39 |
3 files changed, 48 insertions, 1 deletions
diff --git a/doc/build/changelog/unreleased_12/4295.rst b/doc/build/changelog/unreleased_12/4295.rst new file mode 100644 index 000000000..acfc8a074 --- /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 98747c680..272fed3e2 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 42a29d13c..cf1eb40c9 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 |