Refs #29227 -- Made inspectdb generate BooleanField(null=True) rather than NullBooleanField.

This commit is contained in:
Tim Graham 2018-03-16 10:52:04 -04:00
parent 5fa4f40f45
commit 6421bd702f
6 changed files with 13 additions and 36 deletions

View File

@ -142,11 +142,8 @@ class Command(BaseCommand):
# Add 'null' and 'blank', if the 'null_ok' flag was present in the
# table description.
if row[6]: # If it's NULL...
if field_type == 'BooleanField(':
field_type = 'NullBooleanField('
else:
extra_params['blank'] = True
extra_params['null'] = True
extra_params['blank'] = True
extra_params['null'] = True
field_desc = '%s = %s%s' % (
att_name,

View File

@ -141,6 +141,10 @@ class BaseDatabaseFeatures:
# Can the backend introspect a TimeField, instead of a DateTimeField?
can_introspect_time_field = True
# Some backends may not be able to differentiate BooleanField from other
# fields such as IntegerField.
introspected_boolean_field_type = 'BooleanField'
# Can the backend introspect the column order (ASC/DESC) for indexes?
supports_index_column_ordering = True
@ -268,17 +272,3 @@ class BaseDatabaseFeatures:
except NotSupportedError:
return False
return True
def introspected_boolean_field_type(self, field=None):
"""
What is the type returned when the backend introspects a BooleanField?
The `field` argument may be used to give further details of the field
to be introspected.
The return value from this function is compared by tests against actual
introspection results; it should provide expectations, not run an
introspection itself.
"""
if field and field.null:
return 'NullBooleanField'
return 'BooleanField'

View File

@ -18,6 +18,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
can_introspect_binary_field = False
can_introspect_small_integer_field = True
can_introspect_positive_integer_field = True
introspected_boolean_field_type = 'IntegerField'
supports_index_column_ordering = False
supports_timezones = False
requires_explicit_null_ordering_when_grouping = True
@ -68,9 +69,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
cursor.execute("SELECT 1 FROM mysql.time_zone LIMIT 1")
return cursor.fetchone() is not None
def introspected_boolean_field_type(self, *args, **kwargs):
return 'IntegerField'
@cached_property
def is_sql_auto_is_null_enabled(self):
with self.connection.cursor() as cursor:

View File

@ -285,6 +285,9 @@ Database backend API
* ``DatabaseOperations.distinct_sql()`` now requires an additional ``params``
argument and returns a tuple of SQL and parameters instead of a SQL string.
* ``DatabaseFeatures.introspected_boolean_field_type`` is changed from a method
to a property.
:mod:`django.contrib.gis`
-------------------------

View File

@ -7,8 +7,6 @@ from django.db import connection
from django.db.backends.base.introspection import TableInfo
from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature
from .models import ColumnTypes
def inspectdb_tables_only(table_name):
"""
@ -96,15 +94,9 @@ class InspectDBTestCase(TestCase):
else:
assertFieldType('big_int_field', "models.IntegerField()")
bool_field = ColumnTypes._meta.get_field('bool_field')
bool_field_type = connection.features.introspected_boolean_field_type(bool_field)
bool_field_type = connection.features.introspected_boolean_field_type
assertFieldType('bool_field', "models.{}()".format(bool_field_type))
null_bool_field = ColumnTypes._meta.get_field('null_bool_field')
null_bool_field_type = connection.features.introspected_boolean_field_type(null_bool_field)
if 'BooleanField' in null_bool_field_type:
assertFieldType('null_bool_field', "models.{}()".format(null_bool_field_type))
else:
assertFieldType('null_bool_field', "models.{}(blank=True, null=True)".format(null_bool_field_type))
assertFieldType('null_bool_field', 'models.{}(blank=True, null=True)'.format(bool_field_type))
if connection.features.can_introspect_decimal_field:
assertFieldType('decimal_field', "models.DecimalField(max_digits=6, decimal_places=1)")

View File

@ -476,10 +476,7 @@ class SchemaTests(TransactionTestCase):
columns = self.column_classes(Author)
# BooleanField are stored as TINYINT(1) on MySQL.
field_type = columns['awesome'][0]
self.assertEqual(
field_type,
connection.features.introspected_boolean_field_type(new_field)
)
self.assertEqual(field_type, connection.features.introspected_boolean_field_type)
def test_add_field_default_transform(self):
"""