diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index 39ec6eacfc..626d095624 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -1191,9 +1191,15 @@ class SQLInsertCompiler(SQLCompiler): 'can only be used to update, not to insert.' % (value, field) ) 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: - 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: value = field.get_db_prep_save(value, connection=self.connection) return value @@ -1356,9 +1362,15 @@ class SQLUpdateCompiler(SQLCompiler): if hasattr(val, 'resolve_expression'): val = val.resolve_expression(self.query, allow_joins=False, for_save=True) 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: - 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'): if field.remote_field: val = field.get_db_prep_save( diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py index e66dcd6297..cc03e83ff4 100644 --- a/tests/expressions/tests.py +++ b/tests/expressions/tests.py @@ -264,7 +264,8 @@ class BasicExpressionsTests(TestCase): def test_object_create_with_aggregate(self): # 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( name='Company', num_employees=Max(Value(1)), num_chairs=1, ceo=Employee.objects.create(firstname="Just", lastname="Doit", salary=30), diff --git a/tests/expressions_window/tests.py b/tests/expressions_window/tests.py index d9a4a4fa6c..129912b93a 100644 --- a/tests/expressions_window/tests.py +++ b/tests/expressions_window/tests.py @@ -670,7 +670,12 @@ class WindowFunctionTests(TestCase): def test_fail_update(self): """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=).' + ) with self.assertRaisesMessage(FieldError, msg): Employee.objects.filter(department='Management').update( salary=Window(expression=Max('salary'), partition_by='department'), @@ -678,7 +683,10 @@ class WindowFunctionTests(TestCase): def test_fail_insert(self): """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=