diff --git a/django/db/backends/mysql/features.py b/django/db/backends/mysql/features.py index 89cb2c00db..ef02e28bb8 100644 --- a/django/db/backends/mysql/features.py +++ b/django/db/backends/mysql/features.py @@ -17,6 +17,7 @@ class DatabaseFeatures(BaseDatabaseFeatures): can_introspect_autofield = True can_introspect_binary_field = False can_introspect_small_integer_field = True + can_introspect_positive_integer_field = True supports_index_column_ordering = False supports_timezones = False requires_explicit_null_ordering_when_grouping = True diff --git a/django/db/backends/mysql/introspection.py b/django/db/backends/mysql/introspection.py index 9e72bdc1f8..8f61fa3061 100644 --- a/django/db/backends/mysql/introspection.py +++ b/django/db/backends/mysql/introspection.py @@ -10,8 +10,8 @@ from django.db.models.indexes import Index from django.utils.datastructures import OrderedSet from django.utils.deprecation import RemovedInDjango21Warning -FieldInfo = namedtuple('FieldInfo', FieldInfo._fields + ('extra',)) -InfoLine = namedtuple('InfoLine', 'col_name data_type max_len num_prec num_scale extra column_default') +FieldInfo = namedtuple('FieldInfo', FieldInfo._fields + ('extra', 'is_unsigned')) +InfoLine = namedtuple('InfoLine', 'col_name data_type max_len num_prec num_scale extra column_default is_unsigned') class DatabaseIntrospection(BaseDatabaseIntrospection): @@ -45,7 +45,11 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): return 'AutoField' elif field_type == 'BigIntegerField': return 'BigAutoField' - + if description.is_unsigned: + if field_type == 'IntegerField': + return 'PositiveIntegerField' + elif field_type == 'SmallIntegerField': + return 'PositiveSmallIntegerField' return field_type def get_table_list(self, cursor): @@ -65,8 +69,13 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): # - precision and scale (for decimal fields) (#5014) # - auto_increment is not available in cursor.description cursor.execute(""" - SELECT column_name, data_type, character_maximum_length, numeric_precision, - numeric_scale, extra, column_default + SELECT + column_name, data_type, character_maximum_length, + numeric_precision, numeric_scale, extra, column_default, + CASE + WHEN column_type LIKE '%% unsigned' THEN 1 + ELSE 0 + END AS is_unsigned FROM information_schema.columns WHERE table_name = %s AND table_schema = DATABASE()""", [table_name]) field_info = {line[0]: InfoLine(*line) for line in cursor.fetchall()} @@ -90,6 +99,7 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): line[6], field_info[col_name].column_default, field_info[col_name].extra, + field_info[col_name].is_unsigned, ) )) ) diff --git a/docs/releases/2.0.txt b/docs/releases/2.0.txt index 1c26193456..4592d8b76d 100644 --- a/docs/releases/2.0.txt +++ b/docs/releases/2.0.txt @@ -159,7 +159,8 @@ Internationalization Management Commands ~~~~~~~~~~~~~~~~~~~ -* ... +* :djadmin:`inspectdb` now translates MySQL's unsigned integer columns to + ``PositiveIntegerField`` or ``PositiveSmallIntegerField``. Migrations ~~~~~~~~~~