From 5cff2cb4c09363ee871d1993e3acbad6fb96c05b Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Sun, 14 May 2017 18:34:45 +0200 Subject: [PATCH] Refs #27859 -- Refactored BaseDatabaseValidation to use check_field_type(). Thanks Tim Graham for the review. --- django/db/backends/base/validation.py | 17 ++++++++++++++++- django/db/backends/mysql/validation.py | 23 ++--------------------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/django/db/backends/base/validation.py b/django/db/backends/base/validation.py index be68bffb11..a02780a694 100644 --- a/django/db/backends/base/validation.py +++ b/django/db/backends/base/validation.py @@ -7,4 +7,19 @@ class BaseDatabaseValidation: return [] def check_field(self, field, **kwargs): - return [] + errors = [] + # Backends may implement a check_field_type() method. + if (hasattr(self, 'check_field_type') and + # Ignore any related fields. + not getattr(field, 'remote_field', None)): + # Ignore fields with unsupported features. + db_supports_all_required_features = all( + getattr(self.connection.features, feature, False) + for feature in field.model._meta.required_db_features + ) + if db_supports_all_required_features: + field_type = field.db_type(self.connection) + # Ignore non-concrete fields. + if field_type is not None: + errors.extend(self.check_field_type(field, field_type)) + return errors diff --git a/django/db/backends/mysql/validation.py b/django/db/backends/mysql/validation.py index 358520d517..b4ae265b03 100644 --- a/django/db/backends/mysql/validation.py +++ b/django/db/backends/mysql/validation.py @@ -26,32 +26,13 @@ class DatabaseValidation(BaseDatabaseValidation): )] return [] - def check_field(self, field, **kwargs): + def check_field_type(self, field, field_type): """ MySQL has the following field length restriction: No character (varchar) fields can have a length exceeding 255 characters if they have a unique index on them. """ - errors = super().check_field(field, **kwargs) - - # Ignore any related fields. - if getattr(field, 'remote_field', None): - return errors - - # Ignore fields with unsupported features. - db_supports_all_required_features = all( - getattr(self.connection.features, feature, False) - for feature in field.model._meta.required_db_features - ) - if not db_supports_all_required_features: - return errors - - field_type = field.db_type(self.connection) - - # Ignore non-concrete fields. - if field_type is None: - return errors - + errors = [] if (field_type.startswith('varchar') and field.unique and (field.max_length is None or int(field.max_length) > 255)): errors.append(