Refs #29227 -- Made inspectdb generate BooleanField(null=True) rather than NullBooleanField.
This commit is contained in:
parent
5fa4f40f45
commit
6421bd702f
|
@ -142,9 +142,6 @@ class Command(BaseCommand):
|
||||||
# Add 'null' and 'blank', if the 'null_ok' flag was present in the
|
# Add 'null' and 'blank', if the 'null_ok' flag was present in the
|
||||||
# table description.
|
# table description.
|
||||||
if row[6]: # If it's NULL...
|
if row[6]: # If it's NULL...
|
||||||
if field_type == 'BooleanField(':
|
|
||||||
field_type = 'NullBooleanField('
|
|
||||||
else:
|
|
||||||
extra_params['blank'] = True
|
extra_params['blank'] = True
|
||||||
extra_params['null'] = True
|
extra_params['null'] = True
|
||||||
|
|
||||||
|
|
|
@ -141,6 +141,10 @@ class BaseDatabaseFeatures:
|
||||||
# Can the backend introspect a TimeField, instead of a DateTimeField?
|
# Can the backend introspect a TimeField, instead of a DateTimeField?
|
||||||
can_introspect_time_field = True
|
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?
|
# Can the backend introspect the column order (ASC/DESC) for indexes?
|
||||||
supports_index_column_ordering = True
|
supports_index_column_ordering = True
|
||||||
|
|
||||||
|
@ -268,17 +272,3 @@ class BaseDatabaseFeatures:
|
||||||
except NotSupportedError:
|
except NotSupportedError:
|
||||||
return False
|
return False
|
||||||
return True
|
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'
|
|
||||||
|
|
|
@ -18,6 +18,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
|
||||||
can_introspect_binary_field = False
|
can_introspect_binary_field = False
|
||||||
can_introspect_small_integer_field = True
|
can_introspect_small_integer_field = True
|
||||||
can_introspect_positive_integer_field = True
|
can_introspect_positive_integer_field = True
|
||||||
|
introspected_boolean_field_type = 'IntegerField'
|
||||||
supports_index_column_ordering = False
|
supports_index_column_ordering = False
|
||||||
supports_timezones = False
|
supports_timezones = False
|
||||||
requires_explicit_null_ordering_when_grouping = True
|
requires_explicit_null_ordering_when_grouping = True
|
||||||
|
@ -68,9 +69,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
|
||||||
cursor.execute("SELECT 1 FROM mysql.time_zone LIMIT 1")
|
cursor.execute("SELECT 1 FROM mysql.time_zone LIMIT 1")
|
||||||
return cursor.fetchone() is not None
|
return cursor.fetchone() is not None
|
||||||
|
|
||||||
def introspected_boolean_field_type(self, *args, **kwargs):
|
|
||||||
return 'IntegerField'
|
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def is_sql_auto_is_null_enabled(self):
|
def is_sql_auto_is_null_enabled(self):
|
||||||
with self.connection.cursor() as cursor:
|
with self.connection.cursor() as cursor:
|
||||||
|
|
|
@ -285,6 +285,9 @@ Database backend API
|
||||||
* ``DatabaseOperations.distinct_sql()`` now requires an additional ``params``
|
* ``DatabaseOperations.distinct_sql()`` now requires an additional ``params``
|
||||||
argument and returns a tuple of SQL and parameters instead of a SQL string.
|
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`
|
:mod:`django.contrib.gis`
|
||||||
-------------------------
|
-------------------------
|
||||||
|
|
||||||
|
|
|
@ -7,8 +7,6 @@ from django.db import connection
|
||||||
from django.db.backends.base.introspection import TableInfo
|
from django.db.backends.base.introspection import TableInfo
|
||||||
from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature
|
from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature
|
||||||
|
|
||||||
from .models import ColumnTypes
|
|
||||||
|
|
||||||
|
|
||||||
def inspectdb_tables_only(table_name):
|
def inspectdb_tables_only(table_name):
|
||||||
"""
|
"""
|
||||||
|
@ -96,15 +94,9 @@ class InspectDBTestCase(TestCase):
|
||||||
else:
|
else:
|
||||||
assertFieldType('big_int_field', "models.IntegerField()")
|
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_type = connection.features.introspected_boolean_field_type(bool_field)
|
|
||||||
assertFieldType('bool_field', "models.{}()".format(bool_field_type))
|
assertFieldType('bool_field', "models.{}()".format(bool_field_type))
|
||||||
null_bool_field = ColumnTypes._meta.get_field('null_bool_field')
|
assertFieldType('null_bool_field', 'models.{}(blank=True, null=True)'.format(bool_field_type))
|
||||||
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))
|
|
||||||
|
|
||||||
if connection.features.can_introspect_decimal_field:
|
if connection.features.can_introspect_decimal_field:
|
||||||
assertFieldType('decimal_field', "models.DecimalField(max_digits=6, decimal_places=1)")
|
assertFieldType('decimal_field', "models.DecimalField(max_digits=6, decimal_places=1)")
|
||||||
|
|
|
@ -476,10 +476,7 @@ class SchemaTests(TransactionTestCase):
|
||||||
columns = self.column_classes(Author)
|
columns = self.column_classes(Author)
|
||||||
# BooleanField are stored as TINYINT(1) on MySQL.
|
# BooleanField are stored as TINYINT(1) on MySQL.
|
||||||
field_type = columns['awesome'][0]
|
field_type = columns['awesome'][0]
|
||||||
self.assertEqual(
|
self.assertEqual(field_type, connection.features.introspected_boolean_field_type)
|
||||||
field_type,
|
|
||||||
connection.features.introspected_boolean_field_type(new_field)
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_add_field_default_transform(self):
|
def test_add_field_default_transform(self):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue