diff options
| author | Simon Charette <charette.s@gmail.com> | 2019-10-17 01:57:39 -0400 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-10-24 12:24:53 +0200 |
| commit | 7acef095d73322f45dcceb99afa1a4e50b520479 (patch) | |
| tree | 44390915d053c6789d7263c8c454581909d42fd3 /django/db/backends | |
| parent | e645f27907da0098ee08455d9f1acec14b285f98 (diff) | |
Fixed #23576 -- Implemented multi-alias fast-path deletion in MySQL backend.
This required moving the entirety of DELETE SQL generation to the
compiler where it should have been in the first place and implementing
a specialized compiler on MySQL/MariaDB.
The MySQL compiler relies on the "DELETE table FROM table JOIN" syntax
for queries spanning over multiple tables.
Diffstat (limited to 'django/db/backends')
| -rw-r--r-- | django/db/backends/mysql/compiler.py | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/django/db/backends/mysql/compiler.py b/django/db/backends/mysql/compiler.py index fc74ac1991..96232aff51 100644 --- a/django/db/backends/mysql/compiler.py +++ b/django/db/backends/mysql/compiler.py @@ -14,7 +14,25 @@ class SQLInsertCompiler(compiler.SQLInsertCompiler, SQLCompiler): class SQLDeleteCompiler(compiler.SQLDeleteCompiler, SQLCompiler): - pass + def as_sql(self): + if self.single_alias: + return super().as_sql() + # MySQL and MariaDB < 10.3.2 doesn't support deletion with a subquery + # which is what the default implementation of SQLDeleteCompiler uses + # when multiple tables are involved. Use the MySQL/MariaDB specific + # DELETE table FROM table syntax instead to avoid performing the + # operation in two queries. + result = [ + 'DELETE %s FROM' % self.quote_name_unless_alias( + self.query.get_initial_alias() + ) + ] + from_sql, from_params = self.get_from_clause() + result.extend(from_sql) + where, params = self.compile(self.query.where) + if where: + result.append('WHERE %s' % where) + return ' '.join(result), tuple(from_params) + tuple(params) class SQLUpdateCompiler(compiler.SQLUpdateCompiler, SQLCompiler): |
