MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / delete

Method delete

lib/sqlalchemy/orm/query.py:3210–3287  ·  view source on GitHub ↗

r"""Perform a DELETE with an arbitrary WHERE clause. Deletes rows matched by this query from the database. E.g.:: sess.query(User).filter(User.age == 25).delete(synchronize_session=False) sess.query(User).filter(User.age == 25).delete( sync

(
        self,
        synchronize_session: SynchronizeSessionArgument = "auto",
        delete_args: Optional[Dict[Any, Any]] = None,
    )

Source from the content-addressed store, hash-verified

3208 )
3209
3210 def delete(
3211 self,
3212 synchronize_session: SynchronizeSessionArgument = "auto",
3213 delete_args: Optional[Dict[Any, Any]] = None,
3214 ) -> int:
3215 r"""Perform a DELETE with an arbitrary WHERE clause.
3216
3217 Deletes rows matched by this query from the database.
3218
3219 E.g.::
3220
3221 sess.query(User).filter(User.age == 25).delete(synchronize_session=False)
3222
3223 sess.query(User).filter(User.age == 25).delete(
3224 synchronize_session="evaluate"
3225 )
3226
3227 .. warning::
3228
3229 See the section :ref:`orm_expression_update_delete` for important
3230 caveats and warnings, including limitations when using bulk UPDATE
3231 and DELETE with mapper inheritance configurations.
3232
3233 :param synchronize_session: chooses the strategy to update the
3234 attributes on objects in the session. See the section
3235 :ref:`orm_expression_update_delete` for a discussion of these
3236 strategies.
3237
3238 :param delete_args: Optional dictionary, if present will be passed
3239 to the underlying :func:`_expression.delete` construct as the ``**kw``
3240 for the object. May be used to pass dialect-specific arguments such
3241 as ``mysql_limit``.
3242
3243 .. versionadded:: 2.0.37
3244
3245 :return: the count of rows matched as returned by the database's
3246 "row count" feature.
3247
3248 .. seealso::
3249
3250 :ref:`orm_expression_update_delete`
3251
3252 """ # noqa: E501
3253
3254 bulk_del = BulkDelete(self, delete_args)
3255 if self.dispatch.before_compile_delete:
3256 for fn in self.dispatch.before_compile_delete:
3257 new_query = fn(bulk_del.query, bulk_del)
3258 if new_query is not None:
3259 bulk_del.query = new_query
3260
3261 self = bulk_del.query
3262
3263 delete_ = sql.delete(*self._raw_columns) # type: ignore
3264
3265 if delete_args:
3266 delete_ = delete_.with_dialect_options(**delete_args)
3267

Callers 15

_run_crudMethod · 0.45
delete_stmtFunction · 0.45
delete_from_all_tablesFunction · 0.45
test_delete_singleMethod · 0.45
test_delete_manyMethod · 0.45
test_deleteMethod · 0.45
test_delete_returningMethod · 0.45
runMethod · 0.45

Calls 8

BulkDeleteClass · 0.85
with_dialect_optionsMethod · 0.80
after_bulk_deleteMethod · 0.80
castFunction · 0.50
executeMethod · 0.45
unionMethod · 0.45
closeMethod · 0.45