Commits

Mike Bayer committed 2547b47

- [bug] quoting is applied to the column names
inside the WITH RECURSIVE clause of a
common table expression according to the
quoting rules for the originating Column.
[ticket:2512]

Comments (0)

Files changed (3)

 0.7.9
 =====
 - sql
+  - [bug] quoting is applied to the column names
+    inside the WITH RECURSIVE clause of a 
+    common table expression according to the 
+    quoting rules for the originating Column.
+    [ticket:2512]
+
   - [bug] Fixed regression introduced in 0.7.6 
     whereby the FROM list of a SELECT statement
     could be incorrect in certain "clone+replace"

lib/sqlalchemy/sql/compiler.py

                     col_source = cte.original.selects[0]
                 else:
                     assert False
-                recur_cols = [c.key for c in util.unique_list(col_source.inner_columns)
+                recur_cols = [c for c in 
+                            util.unique_list(col_source.inner_columns)
                                 if c is not None]
 
-                text += "(%s)" % (", ".join(recur_cols))
+                text += "(%s)" % (", ".join(
+                                    self.preparer.format_column(ident) 
+                                    for ident in recur_cols))
             text += " AS \n" + \
                         cte.original._compiler_dispatch(
                                 self, asfrom=True, **kwargs

test/sql/test_compiler.py

             "FROM regional_sales WHERE "
             "regional_sales.amount < :amount_2")
 
+    def test_cte_reserved_quote(self):
+        orders = table('orders', 
+            column('order'),
+        )
+        s = select([orders.c.order]).cte("regional_sales", recursive=True)
+        s = select([s.c.order])
+        self.assert_compile(s,
+            'WITH RECURSIVE regional_sales("order") AS '
+            '(SELECT orders."order" AS "order" '
+            "FROM orders)"
+            ' SELECT regional_sales."order" '
+            "FROM regional_sales"
+            )
+
     def test_date_between(self):
         import datetime
         table = Table('dt', metadata,