From cfc7cd6513a72b8c5898c264d04bdf49f897a1de Mon Sep 17 00:00:00 2001 From: David-Wobrock Date: Fri, 23 Oct 2020 23:56:12 +0200 Subject: [PATCH] Fixed #32132 -- Fixed column types in m2m intermediary tables for Positive(Big/Small)IntegerFields. --- django/db/models/fields/__init__.py | 31 +++++++++++++++++-------- tests/model_fields/test_integerfield.py | 10 ++++++++ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 2bce85beef..c84136680d 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -1864,6 +1864,13 @@ class BigIntegerField(IntegerField): }) +class SmallIntegerField(IntegerField): + description = _('Small integer') + + def get_internal_type(self): + return 'SmallIntegerField' + + class IPAddressField(Field): empty_strings_allowed = False description = _("IPv4 address") @@ -2006,6 +2013,17 @@ class NullBooleanField(BooleanField): class PositiveIntegerRelDbTypeMixin: + def __init_subclass__(cls, **kwargs): + super().__init_subclass__(**kwargs) + if not hasattr(cls, 'integer_field_class'): + cls.integer_field_class = next( + ( + parent + for parent in cls.__mro__[1:] + if issubclass(parent, IntegerField) + ), + None, + ) def rel_db_type(self, connection): """ @@ -2019,10 +2037,10 @@ class PositiveIntegerRelDbTypeMixin: if connection.features.related_fields_match_type: return self.db_type(connection) else: - return IntegerField().db_type(connection=connection) + return self.integer_field_class().db_type(connection=connection) -class PositiveBigIntegerField(PositiveIntegerRelDbTypeMixin, IntegerField): +class PositiveBigIntegerField(PositiveIntegerRelDbTypeMixin, BigIntegerField): description = _('Positive big integer') def get_internal_type(self): @@ -2048,7 +2066,7 @@ class PositiveIntegerField(PositiveIntegerRelDbTypeMixin, IntegerField): }) -class PositiveSmallIntegerField(PositiveIntegerRelDbTypeMixin, IntegerField): +class PositiveSmallIntegerField(PositiveIntegerRelDbTypeMixin, SmallIntegerField): description = _("Positive small integer") def get_internal_type(self): @@ -2094,13 +2112,6 @@ class SlugField(CharField): }) -class SmallIntegerField(IntegerField): - description = _("Small integer") - - def get_internal_type(self): - return "SmallIntegerField" - - class TextField(Field): description = _("Text") diff --git a/tests/model_fields/test_integerfield.py b/tests/model_fields/test_integerfield.py index bc370f18e2..13b18d967c 100644 --- a/tests/model_fields/test_integerfield.py +++ b/tests/model_fields/test_integerfield.py @@ -176,6 +176,11 @@ class BigIntegerFieldTests(IntegerFieldTests): class PositiveSmallIntegerFieldTests(IntegerFieldTests): model = PositiveSmallIntegerModel documented_range = (0, 32767) + rel_db_type_class = ( + models.PositiveSmallIntegerField + if connection.features.related_fields_match_type + else models.SmallIntegerField + ) class PositiveIntegerFieldTests(IntegerFieldTests): @@ -198,6 +203,11 @@ class PositiveIntegerFieldTests(IntegerFieldTests): class PositiveBigIntegerFieldTests(IntegerFieldTests): model = PositiveBigIntegerModel documented_range = (0, 9223372036854775807) + rel_db_type_class = ( + models.PositiveBigIntegerField + if connection.features.related_fields_match_type + else models.BigIntegerField + ) class ValidationTests(SimpleTestCase):