From 6421bd702fb8205c6b91395d087dc34d179f0c35 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Fri, 16 Mar 2018 10:52:04 -0400 Subject: [PATCH] Refs #29227 -- Made inspectdb generate BooleanField(null=True) rather than NullBooleanField. --- django/core/management/commands/inspectdb.py | 7 ++----- django/db/backends/base/features.py | 18 ++++-------------- django/db/backends/mysql/features.py | 4 +--- docs/releases/2.1.txt | 3 +++ tests/inspectdb/tests.py | 12 ++---------- tests/schema/tests.py | 5 +---- 6 files changed, 13 insertions(+), 36 deletions(-) diff --git a/django/core/management/commands/inspectdb.py b/django/core/management/commands/inspectdb.py index 0834a17c19c..03c8749b905 100644 --- a/django/core/management/commands/inspectdb.py +++ b/django/core/management/commands/inspectdb.py @@ -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, diff --git a/django/db/backends/base/features.py b/django/db/backends/base/features.py index 7d3078c1f8a..b86f0be35bc 100644 --- a/django/db/backends/base/features.py +++ b/django/db/backends/base/features.py @@ -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' diff --git a/django/db/backends/mysql/features.py b/django/db/backends/mysql/features.py index eee140e9ffc..9c1500e6925 100644 --- a/django/db/backends/mysql/features.py +++ b/django/db/backends/mysql/features.py @@ -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: diff --git a/docs/releases/2.1.txt b/docs/releases/2.1.txt index ec2a3266a12..439dc275ce2 100644 --- a/docs/releases/2.1.txt +++ b/docs/releases/2.1.txt @@ -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` ------------------------- diff --git a/tests/inspectdb/tests.py b/tests/inspectdb/tests.py index 794ebc26607..db61246c6a4 100644 --- a/tests/inspectdb/tests.py +++ b/tests/inspectdb/tests.py @@ -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)") diff --git a/tests/schema/tests.py b/tests/schema/tests.py index 3a66c2ae7ad..ab8acbbb993 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -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): """