-
Notifications
You must be signed in to change notification settings - Fork 470
fix(queryset): use subquery for DELETE/UPDATE filtering by related fields #2139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1309,6 +1309,26 @@ def _make_query(self) -> None: | |
| self.resolve_ordering(self.model, table, self._orderings, self._annotations) | ||
|
|
||
| self.resolve_filters() | ||
| if self._joined_tables: | ||
| # If we have joins, we must use a subquery for update | ||
| # because standard UPDATE does not support JOINs on many DBs. | ||
| pk_column = self.model._meta.db_pk_column | ||
| subquery = self._db.query_class.from_(table).select(table[pk_column]) | ||
| subquery._wheres = self.query._wheres | ||
| subquery._havings = self.query._havings | ||
| subquery._joins = self.query._joins | ||
| if hasattr(self.query, "_limit"): | ||
| subquery._limit = self.query._limit | ||
| if hasattr(self.query, "_orderbys"): | ||
| subquery._orderbys = self.query._orderbys | ||
|
Comment on lines
+1315
to
+1323
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there reason why you was able to cleanly make subquery based on query in delete, but wasn't able to do it in update? |
||
|
|
||
| # To avoid MySQL Error 1093, we wrap the subquery in another SELECT | ||
| # To avoid MySQL Error 1235, the outer SELECT shouldn't have LIMIT | ||
| wrapper = self._db.query_class.from_(subquery.as_("_t")).select(Table("_t")[pk_column]) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should use this hack only where it is neccessary, you can do somthing like this: |
||
|
|
||
| self.query = self._db.query_class.update(table) | ||
| self.query = self.query.where(table[pk_column].isin(wrapper)) | ||
|
|
||
| for key, value in self.update_kwargs.items(): | ||
| field_object = self.model._meta.fields_map.get(key) | ||
| if not field_object: | ||
|
|
@@ -1391,6 +1411,21 @@ def _make_query(self) -> None: | |
| annotations=self._annotations, | ||
| ) | ||
| self.resolve_filters() | ||
| if self._joined_tables: | ||
| # If we have joins, we must use a subquery for deletion | ||
| # because standard DELETE FROM does not support JOINs. | ||
| pk_column = self.model._meta.db_pk_column | ||
| subquery = self.query.select(self.model._meta.basetable[pk_column]) | ||
|
|
||
| # To avoid MySQL Error 1093, we wrap the subquery in another SELECT | ||
| # To avoid MySQL Error 1235, the outer SELECT shouldn't have LIMIT | ||
| # We use the connection's query class directly to avoid carrying over | ||
| # the base table into the FROM clause. | ||
| wrapper = self._db.query_class.from_(subquery.as_("_t")).select(Table("_t")[pk_column]) | ||
|
|
||
| self.query = copy(self.model._meta.basequery) | ||
| self.query = self.query.where(self.model._meta.basetable[pk_column].isin(wrapper)) | ||
|
|
||
| self.query._delete_from = True | ||
| return | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add some test, that tests behavior in cases where there are limit and order by clauses, to see that subquery will update/delete only items that fall into subquery