]> 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:41:09 +0000 (11:41 -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
(cherry picked from commit 926952c4afe0b2e16c4a74f05958bded7b932760)

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 c418d1542aa51ee608addb1d8fe43f3e7a67aea3..c727b03b4ec02e573b57765e669b55770b717876 100644 (file)
@@ -2957,7 +2957,8 @@ class Select(HasPrefixes, HasSuffixes, GenerativeSelect):
                 self._distinct = True
             else:
                 self._distinct = [
-                    _literal_as_text(e) for e in util.to_list(distinct)
+                    _literal_as_label_reference(e)
+                    for e in util.to_list(distinct)
                 ]
 
         if from_obj is not None:
index 46493cdc1bb647e0daba4180447316b1c5eb77cc..b960fa47a8cbf9916351099d7f16163fe87ea1a7 100644 (file)
@@ -698,6 +698,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