# QuerySet ".update(...)" silently turns into SELECT+UPDATE? (MySQL)

**URL:** <https://forum.djangoproject.com/t/queryset-update-silently-turns-into-select-update-mysql/39095>\
**Category:** ORM\
**Created:** [February 24, 2025, 2:36pm UTC](https://forum.djangoproject.com/t/queryset-update-silently-turns-into-select-update-mysql/39095 "2025-02-24T14:36:55Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![charettes](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/charettes/32/27_2.png) [@charettes](https://forum.djangoproject.com/u/charettes)\
**Post date:** [April 7, 2025, 3:27am UTC](https://forum.djangoproject.com/t/queryset-update-silently-turns-into-select-update-mysql/39095/7 "2025-04-07T03:27:46Z")

</div>

Thanks for seeing this through @bbkfhq 🙇

For anyone interested in preventing this problem from happening in the first place on MySQL and potentially modernizing the SQL used to perform updates involving related tables (and [even possibly adding support for aggregation](https://code.djangoproject.com/ticket/25643)) [I’ve demonstrated](https://code.djangoproject.com/ticket/36213#comment:8) that it should not be too hard to adjust `SQLUpdateCompiler` to take advantage of the non-standardized `UPDATE FROM` extension supported [on Postgres, SQLite, and MySQL in some flavor](https://www.sqlite.org/lang_update.html#update_from_in_other_sql_database_engines).

This [branch](https://github.com/django/django/compare/main...charettes:django:mysql-update-tweaks) includes three improvements

1. The first one [avoids the Python-level materialization problem on MySQL](https://github.com/django/django/commit/7bf7934affe737cb0a690faa5ac12f0f5aebed31) entirely by forcing a database level materialization. This is a strategy [we used for deletes](https://github.com/django/django/pull/13368/commits/f6405c0b8ef7aff513b105c1da68407a881a3671) successfully and reduces the race condition significantly (or would entirely if `FOR UPDATE` was used).
2. The second [dip toes in a MySQL flavor of `UPDATE FROM` by demonstrating it can be done](https://github.com/django/django/commit/7e0623ce23ea1ed42e1130304a07f6149ab31656). We’d want a database agnostic solution here though that is based on a feature flag (e.g. `supports_update_from` that would be disabled on Oracle, and SQLite \< 3.33).
3. The third demonstrates how the MySQL flavor of `UPDATE FROM` (which supports updating many tables in a single statement which is pretty wild) can be used to support MTI updates in a single statement even when `RETURNING` isn’t supported (which is the case on MySQL).

I think 1. should be considered a strong contender for merging as-is because it addresses a technically avoidable foot-gun and 2. is a good basis for anyone interested in learning more about the ORM (3. remains more a PoC than anythings). I’d be happy to propose a feature request for 1. if it gets support here and sponsor / mentor a potential contributor with a reasonable experience with SQL to see 2. through.

---

_[View the full topic](https://forum.djangoproject.com/t/queryset-update-silently-turns-into-select-update-mysql/39095)._
