diff options
| author | Hannes Ljungberg <hannes.ljungberg@gmail.com> | 2021-05-25 21:17:05 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-06-30 10:08:55 +0200 |
| commit | d8c90d4c22cb218f1c170eba086c53d3dff7fbc0 (patch) | |
| tree | c330f4a71f3cc41621d19b593d60b442488c7b38 /django/db/models/sql | |
| parent | 053141d31fe5aef1c255a1be183383860e0ccce9 (diff) | |
Fixed #32786 -- Moved subquery ordering clearing optimization to the _in lookup.
Co-Authored-By: Simon Charette <charette.s@gmail.com>
Diffstat (limited to 'django/db/models/sql')
| -rw-r--r-- | django/db/models/sql/query.py | 13 | ||||
| -rw-r--r-- | django/db/models/sql/where.py | 1 |
2 files changed, 8 insertions, 6 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 11c83bfe1a..814271a1f6 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -1034,12 +1034,6 @@ class Query(BaseExpression): # Subqueries need to use a different set of aliases than the outer query. clone.bump_prefix(query) clone.subquery = True - # It's safe to drop ordering if the queryset isn't using slicing, - # distinct(*fields) or select_for_update(). - if (self.low_mark == 0 and self.high_mark is None and - not self.distinct_fields and - not self.select_for_update): - clone.clear_ordering(force=True) clone.where.resolve_expression(query, *args, **kwargs) for key, value in clone.annotations.items(): resolved = value.resolve_expression(query, *args, **kwargs) @@ -1062,6 +1056,13 @@ class Query(BaseExpression): ] def as_sql(self, compiler, connection): + # Some backends (e.g. Oracle) raise an error when a subquery contains + # unnecessary ORDER BY clause. + if ( + self.subquery and + not connection.features.ignores_unnecessary_order_by_in_subqueries + ): + self.clear_ordering(force=False) sql, params = self.get_compiler(connection=connection).as_sql() if self.subquery: sql = '(%s)' % sql diff --git a/django/db/models/sql/where.py b/django/db/models/sql/where.py index 795eff8395..2577e1d7a5 100644 --- a/django/db/models/sql/where.py +++ b/django/db/models/sql/where.py @@ -239,6 +239,7 @@ class SubqueryConstraint: self.alias = alias self.columns = columns self.targets = targets + query_object.clear_ordering(clear_default=True) self.query_object = query_object def as_sql(self, compiler, connection): |
