MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / union

Method union

lib/sqlalchemy/orm/query.py:2128–2173  ·  view source on GitHub ↗

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])

Source from the content-addressed store, hash-verified

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.

Callers 15

_async_soft_closeMethod · 0.45
orm_pre_session_execMethod · 0.45
_key_switchersMethod · 0.45
_update_optsMethod · 0.45
_reconcileMethod · 0.45
_generate_backrefMethod · 0.45
_bulk_updateFunction · 0.45
orm_pre_session_execMethod · 0.45
orm_pre_session_execMethod · 0.45
orm_execute_statementMethod · 0.45
orm_execute_statementMethod · 0.45
refresh_handlerMethod · 0.45

Calls 1

_set_opMethod · 0.95

Tested by

no test coverage detected