.. changelog::
:version: 1.0.14
+ .. change::
+ :tags: bug, sql
+ :tickets: 3722
+
+ Fixed bug in :class:`.CTE` structure which would cause it to not
+ clone properly when a union was used, as is common in a recursive
+ CTE. The improper cloning would cause errors when the CTE is used
+ in various ORM contexts such as that of a :func:`.column_property`.
+
.. change::
:tags: bug, sql
:tickets: 3721
self._suffixes = _suffixes
super(CTE, self).__init__(selectable, name=name)
+ def _copy_internals(self, clone=_clone, **kw):
+ super(CTE, self)._copy_internals(clone, **kw)
+ if self._cte_alias is not None:
+ self._cte_alias = self
+ self._restates = frozenset([
+ clone(elem, **kw) for elem in self._restates
+ ])
+
@util.dependencies("sqlalchemy.sql.dml")
def _populate_column_collection(self, dml):
if isinstance(self.element, dml.UpdateBase):
"FROM table3 AS table3_1"
)
+ def test_cte_w_union(self):
+ t = select([func.values(1).label("n")]).cte("t", recursive=True)
+ t = t.union_all(select([t.c.n + 1]).where(t.c.n < 100))
+ s = select([func.sum(t.c.n)])
+
+ from sqlalchemy.sql.visitors import cloned_traverse
+ cloned = cloned_traverse(s, {}, {})
+
+ self.assert_compile(cloned,
+ "WITH RECURSIVE t(n) AS "
+ "(SELECT values(:values_1) AS n "
+ "UNION ALL SELECT t.n + :n_1 AS anon_1 "
+ "FROM t "
+ "WHERE t.n < :n_2) "
+ "SELECT sum(t.n) AS sum_1 FROM t"
+ )
+
def test_text(self):
clause = text(
"select * from table where foo=:bar",