Produce a UNION of this Query against one or more queries. e.g.:: q1 = sess.query(SomeClass).filter(SomeClass.foo == "bar") q2 = sess.query(SomeClass).filter(SomeClass.bar == "foo") q3 = q1.union(q2) The method accepts multiple Query objects so
(self, *q: Query[Any])
| 2126 | return self._from_selectable(expr_fn(*(list_of_queries)).subquery()) |
| 2127 | |
| 2128 | def union(self, *q: Query[Any]) -> Self: |
| 2129 | """Produce a UNION of this Query against one or more queries. |
| 2130 | |
| 2131 | e.g.:: |
| 2132 | |
| 2133 | q1 = sess.query(SomeClass).filter(SomeClass.foo == "bar") |
| 2134 | q2 = sess.query(SomeClass).filter(SomeClass.bar == "foo") |
| 2135 | |
| 2136 | q3 = q1.union(q2) |
| 2137 | |
| 2138 | The method accepts multiple Query objects so as to control |
| 2139 | the level of nesting. A series of ``union()`` calls such as:: |
| 2140 | |
| 2141 | x.union(y).union(z).all() |
| 2142 | |
| 2143 | will nest on each ``union()``, and produces: |
| 2144 | |
| 2145 | .. sourcecode:: sql |
| 2146 | |
| 2147 | SELECT * FROM (SELECT * FROM (SELECT * FROM X UNION |
| 2148 | SELECT * FROM y) UNION SELECT * FROM Z) |
| 2149 | |
| 2150 | Whereas:: |
| 2151 | |
| 2152 | x.union(y, z).all() |
| 2153 | |
| 2154 | produces: |
| 2155 | |
| 2156 | .. sourcecode:: sql |
| 2157 | |
| 2158 | SELECT * FROM (SELECT * FROM X UNION SELECT * FROM y UNION |
| 2159 | SELECT * FROM Z) |
| 2160 | |
| 2161 | Note that many database backends do not allow ORDER BY to |
| 2162 | be rendered on a query called within UNION, EXCEPT, etc. |
| 2163 | To disable all ORDER BY clauses including those configured |
| 2164 | on mappers, issue ``query.order_by(None)`` - the resulting |
| 2165 | :class:`_query.Query` object will not render ORDER BY within |
| 2166 | its SELECT statement. |
| 2167 | |
| 2168 | .. seealso:: |
| 2169 | |
| 2170 | :meth:`_sql.Select.union` - v2 equivalent method. |
| 2171 | |
| 2172 | """ |
| 2173 | return self._set_op(expression.union, *q) |
| 2174 | |
| 2175 | def union_all(self, *q: Query[Any]) -> Self: |
| 2176 | """Produce a UNION ALL of this Query against one or more queries. |
no test coverage detected