Fixed #24365 -- Made inspectdb translate MySQL unsigned integer columns to positive integer fields.

This commit is contained in:
Mariusz Felisiak 2017-03-08 16:56:00 +01:00 committed by Tim Graham
parent af121b08e8
commit 94d8bea212
3 changed files with 18 additions and 6 deletions

View File

@ -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

View File

@ -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,
)
))
)

View File

@ -159,7 +159,8 @@ Internationalization
Management Commands
~~~~~~~~~~~~~~~~~~~
* ...
* :djadmin:`inspectdb` now translates MySQL's unsigned integer columns to
``PositiveIntegerField`` or ``PositiveSmallIntegerField``.
Migrations
~~~~~~~~~~