From 0b0998dc151feb77068e2387c34cc50ef6b356ae Mon Sep 17 00:00:00 2001 From: 4the4ryushin Date: Fri, 28 Apr 2023 02:33:25 +0530 Subject: Fixed #33759 -- Avoided unnecessary subquery in QuerySet.delete() with self-referential subqueries if supported. --- django/db/backends/base/features.py | 3 +++ django/db/backends/mysql/features.py | 1 + django/db/models/sql/compiler.py | 5 ++++- 3 files changed, 8 insertions(+), 1 deletion(-) (limited to 'django') diff --git a/django/db/backends/base/features.py b/django/db/backends/base/features.py index 63b3ab7710..11fa807c1b 100644 --- a/django/db/backends/base/features.py +++ b/django/db/backends/base/features.py @@ -12,6 +12,9 @@ class BaseDatabaseFeatures: allows_group_by_select_index = True empty_fetchmany_value = [] update_can_self_select = True + # Does the backend support self-reference subqueries in the DELETE + # statement? + delete_can_self_reference_subquery = True # Does the backend distinguish between '' and None? interprets_empty_strings_as_nulls = False diff --git a/django/db/backends/mysql/features.py b/django/db/backends/mysql/features.py index 0b8d12d2fe..739ab380e0 100644 --- a/django/db/backends/mysql/features.py +++ b/django/db/backends/mysql/features.py @@ -24,6 +24,7 @@ class DatabaseFeatures(BaseDatabaseFeatures): supports_slicing_ordering_in_compound = True supports_index_on_text_field = False supports_update_conflicts = True + delete_can_self_reference_subquery = False create_test_procedure_without_params_sql = """ CREATE PROCEDURE test_procedure () BEGIN diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index 07393e7605..a7a8c98b99 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -1890,7 +1890,10 @@ class SQLDeleteCompiler(SQLCompiler): Create the SQL for this query. Return the SQL string and list of parameters. """ - if self.single_alias and not self.contains_self_reference_subquery: + if self.single_alias and ( + self.connection.features.delete_can_self_reference_subquery + or not self.contains_self_reference_subquery + ): return self._as_sql(self.query) innerq = self.query.clone() innerq.__class__ = Query -- cgit v1.2.1