]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Use label reference coercion for select() distinct keyword argument
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 6 Dec 2019 16:38:55 +0000 (11:38 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 6 Dec 2019 16:39:32 +0000 (11:39 -0500)
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

doc/build/changelog/unreleased_13/5028.rst [new file with mode: 0644]
lib/sqlalchemy/sql/selectable.py
test/sql/test_text.py

diff --git a/doc/build/changelog/unreleased_13/5028.rst b/doc/build/changelog/unreleased_13/5028.rst
new file mode 100644 (file)
index 0000000..be2a354
--- /dev/null
@@ -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.
+
index 4b3844eec142ccb3f00b44734d72b7bcc2a22266..0eadab610708a85d29f7418623fea238625bb194 100644 (file)
@@ -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)
                     ]
                 )
index 3386d0aae952610f5bbdd9b595196bb418c75174..ae4be65ad6cfc81a26d4ebc72817961f5a4f5f1c 100644 (file)
@@ -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