mirror of
				https://github.com/django/django.git
				synced 2025-10-28 16:16:12 +00:00 
			
		
		
		
	[4.2.x] Fixed #34372 -- Fixed queryset crash on order by aggregation using OrderBy.
Regression in278881e376caused by a lack of expression copying when an OrderBy expression is explicitly provided. Thanks Jannis Vajen for the report and regression test. Backport ofb15f162f25from main
This commit is contained in:
		
				
					committed by
					
						 Mariusz Felisiak
						Mariusz Felisiak
					
				
			
			
				
	
			
			
			
						parent
						
							5a82acbda8
						
					
				
				
					commit
					872dade29c
				
			| @@ -358,11 +358,13 @@ class SQLCompiler: | |||||||
|                     if ( |                     if ( | ||||||
|                         field.nulls_first is None and field.nulls_last is None |                         field.nulls_first is None and field.nulls_last is None | ||||||
|                     ) or self.connection.features.supports_order_by_nulls_modifier: |                     ) or self.connection.features.supports_order_by_nulls_modifier: | ||||||
|  |                         field = field.copy() | ||||||
|                         field.expression = select_ref |                         field.expression = select_ref | ||||||
|                     # Alias collisions are not possible when dealing with |                     # Alias collisions are not possible when dealing with | ||||||
|                     # combined queries so fallback to it if emulation of NULLS |                     # combined queries so fallback to it if emulation of NULLS | ||||||
|                     # handling is required. |                     # handling is required. | ||||||
|                     elif self.query.combinator: |                     elif self.query.combinator: | ||||||
|  |                         field = field.copy() | ||||||
|                         field.expression = Ref(select_ref.refs, select_ref.source) |                         field.expression = Ref(select_ref.refs, select_ref.source) | ||||||
|                 yield field, select_ref is not None |                 yield field, select_ref is not None | ||||||
|                 continue |                 continue | ||||||
|   | |||||||
| @@ -638,3 +638,9 @@ class OrderingTests(TestCase): | |||||||
|             .first(), |             .first(), | ||||||
|             self.a1, |             self.a1, | ||||||
|         ) |         ) | ||||||
|  |  | ||||||
|  |     def test_order_by_expr_query_reuse(self): | ||||||
|  |         qs = Author.objects.annotate(num=Count("article")).order_by( | ||||||
|  |             F("num").desc(), "pk" | ||||||
|  |         ) | ||||||
|  |         self.assertCountEqual(qs, qs.iterator()) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user