backport distinct(*expr) to 0.6

Issue #2150 resolved
Former user created an issue

Backport of distinct(*expr) to 0.6

diff -r fb9aba2155be4da60a54b707f3b40dc2853a1e6d lib/sqlalchemy/sql/expression.py
--- a/lib/sqlalchemy/sql/expression.py  Sat Apr 23 15:13:45 2011 -0400
+++ b/lib/sqlalchemy/sql/expression.py  Tue Apr 26 17:25:26 2011 -0500
@@ -4267,12 +4267,19 @@
         self.append_having(having)

     @_generative
-    def distinct(self):
+    def distinct(self, *expr):
         """return a new select() construct which will apply DISTINCT to its
         columns clause.

          """
-        self._distinct = True
+        if expr:
+            expr = [for e in expr](_literal_as_text(e))
+            if isinstance(self._distinct, list):
+                self._distinct = self._distinct + expr
+            else:
+                self._distinct = expr
+        else:
+            self._distinct = True

     @_generative
     def prefix_with(self, clause):

See #1069 for the original patch.

Also please consider adding the related patch for Query objects as well.

Comments (3)

  1. Mike Bayer repo owner
    • changed component to sql

    this is the actual patch: 3f9a343d725eea883aba2145de4cbb7b84f9d08c

    its in 0.7 because it deprecates several features, and greatly reorganizes how "distinct" is handled at all levels.

    the patch above includes:

    • documentation
    • deprecation for MySQL's old style
    • deprecation for passing a basestring to select(distinct='xyz')
    • fixes to the logic used in compiler.py
    • fixes to PG's logic
    • tests for the feature, PGs behavior, MySQLs behavior

    im not yet convinced 0.6 really needs a new feature backport of this size - 0.7.0 is due very soon.

  2. Log in to comment