mirror of
				https://github.com/django/django.git
				synced 2025-10-31 01:25:32 +00:00 
			
		
		
		
	Fixed #29619 -- Added field names to some FieldErrors.
This commit is contained in:
		
				
					committed by
					
						 Tim Graham
						Tim Graham
					
				
			
			
				
	
			
			
			
						parent
						
							5013d38380
						
					
				
				
					commit
					741ce81a42
				
			| @@ -1191,9 +1191,15 @@ class SQLInsertCompiler(SQLCompiler): | |||||||
|                     'can only be used to update, not to insert.' % (value, field) |                     'can only be used to update, not to insert.' % (value, field) | ||||||
|                 ) |                 ) | ||||||
|             if value.contains_aggregate: |             if value.contains_aggregate: | ||||||
|                 raise FieldError("Aggregate functions are not allowed in this query") |                 raise FieldError( | ||||||
|  |                     'Aggregate functions are not allowed in this query ' | ||||||
|  |                     '(%s=%r).' % (field.name, value) | ||||||
|  |                 ) | ||||||
|             if value.contains_over_clause: |             if value.contains_over_clause: | ||||||
|                 raise FieldError('Window expressions are not allowed in this query.') |                 raise FieldError( | ||||||
|  |                     'Window expressions are not allowed in this query (%s=%r).' | ||||||
|  |                     % (field.name, value) | ||||||
|  |                 ) | ||||||
|         else: |         else: | ||||||
|             value = field.get_db_prep_save(value, connection=self.connection) |             value = field.get_db_prep_save(value, connection=self.connection) | ||||||
|         return value |         return value | ||||||
| @@ -1356,9 +1362,15 @@ class SQLUpdateCompiler(SQLCompiler): | |||||||
|             if hasattr(val, 'resolve_expression'): |             if hasattr(val, 'resolve_expression'): | ||||||
|                 val = val.resolve_expression(self.query, allow_joins=False, for_save=True) |                 val = val.resolve_expression(self.query, allow_joins=False, for_save=True) | ||||||
|                 if val.contains_aggregate: |                 if val.contains_aggregate: | ||||||
|                     raise FieldError("Aggregate functions are not allowed in this query") |                     raise FieldError( | ||||||
|  |                         'Aggregate functions are not allowed in this query ' | ||||||
|  |                         '(%s=%r).' % (field.name, val) | ||||||
|  |                     ) | ||||||
|                 if val.contains_over_clause: |                 if val.contains_over_clause: | ||||||
|                     raise FieldError('Window expressions are not allowed in this query.') |                     raise FieldError( | ||||||
|  |                         'Window expressions are not allowed in this query ' | ||||||
|  |                         '(%s=%r).' % (field.name, val) | ||||||
|  |                     ) | ||||||
|             elif hasattr(val, 'prepare_database_save'): |             elif hasattr(val, 'prepare_database_save'): | ||||||
|                 if field.remote_field: |                 if field.remote_field: | ||||||
|                     val = field.get_db_prep_save( |                     val = field.get_db_prep_save( | ||||||
|   | |||||||
| @@ -264,7 +264,8 @@ class BasicExpressionsTests(TestCase): | |||||||
|  |  | ||||||
|     def test_object_create_with_aggregate(self): |     def test_object_create_with_aggregate(self): | ||||||
|         # Aggregates are not allowed when inserting new data |         # Aggregates are not allowed when inserting new data | ||||||
|         with self.assertRaisesMessage(FieldError, 'Aggregate functions are not allowed in this query'): |         msg = 'Aggregate functions are not allowed in this query (num_employees=Max(Value(1))).' | ||||||
|  |         with self.assertRaisesMessage(FieldError, msg): | ||||||
|             Company.objects.create( |             Company.objects.create( | ||||||
|                 name='Company', num_employees=Max(Value(1)), num_chairs=1, |                 name='Company', num_employees=Max(Value(1)), num_chairs=1, | ||||||
|                 ceo=Employee.objects.create(firstname="Just", lastname="Doit", salary=30), |                 ceo=Employee.objects.create(firstname="Just", lastname="Doit", salary=30), | ||||||
|   | |||||||
| @@ -670,7 +670,12 @@ class WindowFunctionTests(TestCase): | |||||||
|  |  | ||||||
|     def test_fail_update(self): |     def test_fail_update(self): | ||||||
|         """Window expressions can't be used in an UPDATE statement.""" |         """Window expressions can't be used in an UPDATE statement.""" | ||||||
|         msg = 'Window expressions are not allowed in this query' |         msg = ( | ||||||
|  |             'Window expressions are not allowed in this query (salary=<Window: ' | ||||||
|  |             'Max(Col(expressions_window_employee, expressions_window.Employee.salary)) ' | ||||||
|  |             'OVER (PARTITION BY Col(expressions_window_employee, ' | ||||||
|  |             'expressions_window.Employee.department))>).' | ||||||
|  |         ) | ||||||
|         with self.assertRaisesMessage(FieldError, msg): |         with self.assertRaisesMessage(FieldError, msg): | ||||||
|             Employee.objects.filter(department='Management').update( |             Employee.objects.filter(department='Management').update( | ||||||
|                 salary=Window(expression=Max('salary'), partition_by='department'), |                 salary=Window(expression=Max('salary'), partition_by='department'), | ||||||
| @@ -678,7 +683,10 @@ class WindowFunctionTests(TestCase): | |||||||
|  |  | ||||||
|     def test_fail_insert(self): |     def test_fail_insert(self): | ||||||
|         """Window expressions can't be used in an INSERT statement.""" |         """Window expressions can't be used in an INSERT statement.""" | ||||||
|         msg = 'Window expressions are not allowed in this query' |         msg = ( | ||||||
|  |             'Window expressions are not allowed in this query (salary=<Window: ' | ||||||
|  |             'Sum(Value(10000), order_by=OrderBy(F(pk), descending=False)) OVER ()' | ||||||
|  |         ) | ||||||
|         with self.assertRaisesMessage(FieldError, msg): |         with self.assertRaisesMessage(FieldError, msg): | ||||||
|             Employee.objects.create( |             Employee.objects.create( | ||||||
|                 name='Jameson', department='Management', hire_date=datetime.date(2007, 7, 1), |                 name='Jameson', department='Management', hire_date=datetime.date(2007, 7, 1), | ||||||
|   | |||||||
| @@ -165,7 +165,11 @@ class AdvancedTests(TestCase): | |||||||
|         self.assertEqual(qs.update(another_value=F('alias')), 3) |         self.assertEqual(qs.update(another_value=F('alias')), 3) | ||||||
|         # Update where aggregation annotation is used in update parameters |         # Update where aggregation annotation is used in update parameters | ||||||
|         qs = DataPoint.objects.annotate(max=Max('value')) |         qs = DataPoint.objects.annotate(max=Max('value')) | ||||||
|         with self.assertRaisesMessage(FieldError, 'Aggregate functions are not allowed in this query'): |         msg = ( | ||||||
|  |             'Aggregate functions are not allowed in this query ' | ||||||
|  |             '(another_value=Max(Col(update_datapoint, update.DataPoint.value))).' | ||||||
|  |         ) | ||||||
|  |         with self.assertRaisesMessage(FieldError, msg): | ||||||
|             qs.update(another_value=F('max')) |             qs.update(another_value=F('max')) | ||||||
|  |  | ||||||
|     def test_update_annotated_multi_table_queryset(self): |     def test_update_annotated_multi_table_queryset(self): | ||||||
| @@ -185,5 +189,9 @@ class AdvancedTests(TestCase): | |||||||
|         # self.assertEqual(updated, 1) |         # self.assertEqual(updated, 1) | ||||||
|         # Update where aggregation annotation is used in update parameters |         # Update where aggregation annotation is used in update parameters | ||||||
|         qs = RelatedPoint.objects.annotate(max=Max('data__value')) |         qs = RelatedPoint.objects.annotate(max=Max('data__value')) | ||||||
|         with self.assertRaisesMessage(FieldError, 'Aggregate functions are not allowed in this query'): |         msg = ( | ||||||
|  |             'Aggregate functions are not allowed in this query ' | ||||||
|  |             '(name=Max(Col(update_datapoint, update.DataPoint.value))).' | ||||||
|  |         ) | ||||||
|  |         with self.assertRaisesMessage(FieldError, msg): | ||||||
|             qs.update(name=F('max')) |             qs.update(name=F('max')) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user