summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2017-05-14 18:34:45 +0200
committerGitHub <noreply@github.com>2017-05-14 18:34:45 +0200
commit5cff2cb4c09363ee871d1993e3acbad6fb96c05b (patch)
treee101c24978d6770f04a1ea4be51d2a8a0fc4a68e
parent680968b9e42d63f9a3ee929ebd6afdef06999d24 (diff)
Refs #27859 -- Refactored BaseDatabaseValidation to use check_field_type().
Thanks Tim Graham for the review.
-rw-r--r--django/db/backends/base/validation.py17
-rw-r--r--django/db/backends/mysql/validation.py23
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(