From 4de31ec680df062e5964b630f1b881ead5004e15 Mon Sep 17 00:00:00 2001 From: toan Date: Thu, 21 Sep 2023 15:51:45 -0700 Subject: [PATCH] Fixed #34858 -- Corrected resolving output_field for PositiveIntegerField. Regression in 40b8a6174f001a310aa33f7880db0efeeb04d4c4. --- AUTHORS | 1 + django/db/models/expressions.py | 19 +++++++++++++++++++ tests/expressions/tests.py | 18 ++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/AUTHORS b/AUTHORS index c29c7170051..ae2bdaf3937 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 4ea179ecde5..30d44650ecf 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 4bd46762e12..f26a6275eae 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),