MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / and_

Function and_

lib/sqlalchemy/sql/_elements_constructors.py:126–188  ·  view source on GitHub ↗

r"""Produce a conjunction of expressions joined by ``AND``. E.g.:: from sqlalchemy import and_ stmt = select(users_table).where( and_(users_table.c.name == "wendy", users_table.c.enrolled == True) ) The :func:`.and_` conjunction is also available using

(  # type: ignore[empty-body]
    initial_clause: Union[Literal[True], _ColumnExpressionArgument[bool]],
    *clauses: _ColumnExpressionArgument[bool],
)

Source from the content-addressed store, hash-verified

124
125
126def and_( # type: ignore[empty-body]
127 initial_clause: Union[Literal[True], _ColumnExpressionArgument[bool]],
128 *clauses: _ColumnExpressionArgument[bool],
129) -> ColumnElement[bool]:
130 r"""Produce a conjunction of expressions joined by ``AND``.
131
132 E.g.::
133
134 from sqlalchemy import and_
135
136 stmt = select(users_table).where(
137 and_(users_table.c.name == "wendy", users_table.c.enrolled == True)
138 )
139
140 The :func:`.and_` conjunction is also available using the
141 Python ``&`` operator (though note that compound expressions
142 need to be parenthesized in order to function with Python
143 operator precedence behavior)::
144
145 stmt = select(users_table).where(
146 (users_table.c.name == "wendy") & (users_table.c.enrolled == True)
147 )
148
149 The :func:`.and_` operation is also implicit in some cases;
150 the :meth:`_expression.Select.where`
151 method for example can be invoked multiple
152 times against a statement, which will have the effect of each
153 clause being combined using :func:`.and_`::
154
155 stmt = (
156 select(users_table)
157 .where(users_table.c.name == "wendy")
158 .where(users_table.c.enrolled == True)
159 )
160
161 The :func:`.and_` construct must be given at least one positional
162 argument in order to be valid; a :func:`.and_` construct with no
163 arguments is ambiguous. To produce an "empty" or dynamically
164 generated :func:`.and_` expression, from a given list of expressions,
165 a "default" element of :func:`_sql.true` (or just ``True``) should be
166 specified::
167
168 from sqlalchemy import true
169
170 criteria = and_(true(), *expressions)
171
172 The above expression will compile to SQL as the expression ``true``
173 or ``1 = 1``, depending on backend, if no other expressions are
174 present. If expressions are present, then the :func:`_sql.true` value is
175 ignored as it does not affect the outcome of an AND expression that
176 has other elements.
177
178 .. deprecated:: 1.4 The :func:`.and_` element now requires that at
179 least one argument is passed; creating the :func:`.and_` construct
180 with no arguments is deprecated, and will emit a deprecation warning
181 while continuing to produce a blank SQL string.
182
183 .. seealso::

Callers 15

test_eightMethod · 0.90
test_nineMethod · 0.90
test_single_bool_oneMethod · 0.90
test_single_bool_twoMethod · 0.90
test_single_bool_sixMethod · 0.90
test_single_bool_nineMethod · 0.90
test_fourMethod · 0.90
test_fiveMethod · 0.90
test_sevenMethod · 0.90
test_nineMethod · 0.90

Calls 1

and_Method · 0.45

Tested by 15

test_eightMethod · 0.72
test_nineMethod · 0.72
test_single_bool_oneMethod · 0.72
test_single_bool_twoMethod · 0.72
test_single_bool_sixMethod · 0.72
test_single_bool_nineMethod · 0.72
test_fourMethod · 0.72
test_fiveMethod · 0.72
test_sevenMethod · 0.72
test_nineMethod · 0.72