diff --git a/AUTHORS b/AUTHORS index c29c717005..ae2bdaf393 100644 --- a/AUTHORS +++ b/AUTHORS @@ -978,6 +978,7 @@ answer newbie questions, and generally made Django that much better: Tim Heap Tim McCurrach Tim Saylor + Toan Vuong Tobias Kunze Tobias McNulty tobias@neuyork.de diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py index 4ea179ecde..30d44650ec 100644 --- a/django/db/models/expressions.py +++ b/django/db/models/expressions.py @@ -512,6 +512,25 @@ class Expression(BaseExpression, Combinable): _connector_combinations = [ # Numeric operations - operands of same type. + # PositiveIntegerField should take precedence over IntegerField (except + # subtraction). + { + connector: [ + ( + fields.PositiveIntegerField, + fields.PositiveIntegerField, + fields.PositiveIntegerField, + ), + ] + for connector in ( + Combinable.ADD, + Combinable.MUL, + Combinable.DIV, + Combinable.MOD, + Combinable.POW, + ) + }, + # Other numeric operands. { connector: [ (fields.IntegerField, fields.IntegerField, fields.IntegerField), diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py index 4bd46762e1..f26a6275ea 100644 --- a/tests/expressions/tests.py +++ b/tests/expressions/tests.py @@ -34,6 +34,7 @@ from django.db.models import ( Model, OrderBy, OuterRef, + PositiveIntegerField, Q, StdDev, Subquery, @@ -2455,6 +2456,23 @@ class CombinableTests(SimpleTestCase): class CombinedExpressionTests(SimpleTestCase): + def test_resolve_output_field_positive_integer(self): + connectors = [ + Combinable.ADD, + Combinable.MUL, + Combinable.DIV, + Combinable.MOD, + Combinable.POW, + ] + for connector in connectors: + with self.subTest(connector=connector): + expr = CombinedExpression( + Expression(PositiveIntegerField()), + connector, + Expression(PositiveIntegerField()), + ) + self.assertIsInstance(expr.output_field, PositiveIntegerField) + def test_resolve_output_field_number(self): tests = [ (IntegerField, AutoField, IntegerField),