r"""Perform an UPDATE with an arbitrary WHERE clause. Updates rows matched by this query in the database. E.g.:: sess.query(User).filter(User.age == 25).update( {User.age: User.age - 10}, synchronize_session=False ) sess.query(U
(
self,
values: Dict[_DMLColumnArgument, Any],
synchronize_session: SynchronizeSessionArgument = "auto",
update_args: Optional[Dict[Any, Any]] = None,
)
| 3229 | return result.rowcount |
| 3230 | |
| 3231 | def update( |
| 3232 | self, |
| 3233 | values: Dict[_DMLColumnArgument, Any], |
| 3234 | synchronize_session: SynchronizeSessionArgument = "auto", |
| 3235 | update_args: Optional[Dict[Any, Any]] = None, |
| 3236 | ) -> int: |
| 3237 | r"""Perform an UPDATE with an arbitrary WHERE clause. |
| 3238 | |
| 3239 | Updates rows matched by this query in the database. |
| 3240 | |
| 3241 | E.g.:: |
| 3242 | |
| 3243 | sess.query(User).filter(User.age == 25).update( |
| 3244 | {User.age: User.age - 10}, synchronize_session=False |
| 3245 | ) |
| 3246 | |
| 3247 | sess.query(User).filter(User.age == 25).update( |
| 3248 | {"age": User.age - 10}, synchronize_session="evaluate" |
| 3249 | ) |
| 3250 | |
| 3251 | .. warning:: |
| 3252 | |
| 3253 | See the section :ref:`orm_expression_update_delete` for important |
| 3254 | caveats and warnings, including limitations when using arbitrary |
| 3255 | UPDATE and DELETE with mapper inheritance configurations. |
| 3256 | |
| 3257 | :param values: a dictionary with attributes names, or alternatively |
| 3258 | mapped attributes or SQL expressions, as keys, and literal |
| 3259 | values or sql expressions as values. If :ref:`parameter-ordered |
| 3260 | mode <tutorial_parameter_ordered_updates>` is desired, the values can |
| 3261 | be passed as a list of 2-tuples; this requires that the |
| 3262 | :paramref:`~sqlalchemy.sql.expression.update.preserve_parameter_order` |
| 3263 | flag is passed to the :paramref:`.Query.update.update_args` dictionary |
| 3264 | as well. |
| 3265 | |
| 3266 | :param synchronize_session: chooses the strategy to update the |
| 3267 | attributes on objects in the session. See the section |
| 3268 | :ref:`orm_expression_update_delete` for a discussion of these |
| 3269 | strategies. |
| 3270 | |
| 3271 | :param update_args: Optional dictionary, if present will be passed |
| 3272 | to the underlying :func:`_expression.update` construct as the ``**kw`` |
| 3273 | for the object. May be used to pass dialect-specific arguments such |
| 3274 | as ``mysql_limit``, as well as other special arguments such as |
| 3275 | :paramref:`~sqlalchemy.sql.expression.update.preserve_parameter_order`. |
| 3276 | |
| 3277 | :return: the count of rows matched as returned by the database's |
| 3278 | "row count" feature. |
| 3279 | |
| 3280 | |
| 3281 | .. seealso:: |
| 3282 | |
| 3283 | :ref:`orm_expression_update_delete` |
| 3284 | |
| 3285 | """ |
| 3286 | |
| 3287 | update_args = update_args or {} |
| 3288 |