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

Function and_

lib/sqlalchemy/sql/_elements_constructors.py:123–185  ·  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

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