From: Mike Bayer Date: Fri, 6 Dec 2019 16:38:55 +0000 (-0500) Subject: Use label reference coercion for select() distinct keyword argument X-Git-Tag: rel_1_4_0b1~603 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=926952c4afe0b2e16c4a74f05958bded7b932760;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Use label reference coercion for select() distinct keyword argument Fixed bug where "distinct" keyword passed to :func:`.select` would not treat a string value as a "label reference" in the same way that the :meth:`.select.distinct` does; it would instead raise unconditionally. This keyword argument and the others passed to :func:`.select` will ultimately be deprecated for SQLAlchemy 2.0. Fixes: #5028 Change-Id: Id36cfe477ed836c3248824ce1b81d0016dbe99f4 --- diff --git a/doc/build/changelog/unreleased_13/5028.rst b/doc/build/changelog/unreleased_13/5028.rst new file mode 100644 index 0000000000..be2a354c25 --- /dev/null +++ b/doc/build/changelog/unreleased_13/5028.rst @@ -0,0 +1,10 @@ +.. change:: + :tags: bug, sql + :tickets: 5028 + + Fixed bug where "distinct" keyword passed to :func:`.select` would not + treat a string value as a "label reference" in the same way that the + :meth:`.select.distinct` does; it would instead raise unconditionally. This + keyword argument and the others passed to :func:`.select` will ultimately + be deprecated for SQLAlchemy 2.0. + diff --git a/lib/sqlalchemy/sql/selectable.py b/lib/sqlalchemy/sql/selectable.py index 4b3844eec1..0eadab6107 100644 --- a/lib/sqlalchemy/sql/selectable.py +++ b/lib/sqlalchemy/sql/selectable.py @@ -3388,7 +3388,7 @@ class Select( if not isinstance(distinct, bool): self._distinct_on = tuple( [ - coercions.expect(roles.WhereHavingRole, e) + coercions.expect(roles.ByOfRole, e) for e in util.to_list(distinct) ] ) diff --git a/test/sql/test_text.py b/test/sql/test_text.py index 3386d0aae9..ae4be65ad6 100644 --- a/test/sql/test_text.py +++ b/test/sql/test_text.py @@ -747,6 +747,15 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): dialect="postgresql", ) + def test_distinct_label_keyword(self): + + stmt = select([table1.c.myid.label("foo")], distinct="foo") + self.assert_compile( + stmt, + "SELECT DISTINCT ON (foo) mytable.myid AS foo FROM mytable", + dialect="postgresql", + ) + def test_unresolvable_distinct_label(self): from sqlalchemy.dialects import postgresql