Refs #27859 -- Refactored BaseDatabaseValidation to use check_field_type().
Thanks Tim Graham for the review.
This commit is contained in:
parent
680968b9e4
commit
5cff2cb4c0
|
@ -7,4 +7,19 @@ class BaseDatabaseValidation:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def check_field(self, field, **kwargs):
|
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
|
||||||
|
|
|
@ -26,32 +26,13 @@ class DatabaseValidation(BaseDatabaseValidation):
|
||||||
)]
|
)]
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def check_field(self, field, **kwargs):
|
def check_field_type(self, field, field_type):
|
||||||
"""
|
"""
|
||||||
MySQL has the following field length restriction:
|
MySQL has the following field length restriction:
|
||||||
No character (varchar) fields can have a length exceeding 255
|
No character (varchar) fields can have a length exceeding 255
|
||||||
characters if they have a unique index on them.
|
characters if they have a unique index on them.
|
||||||
"""
|
"""
|
||||||
errors = super().check_field(field, **kwargs)
|
errors = []
|
||||||
|
|
||||||
# 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
|
|
||||||
|
|
||||||
if (field_type.startswith('varchar') and field.unique and
|
if (field_type.startswith('varchar') and field.unique and
|
||||||
(field.max_length is None or int(field.max_length) > 255)):
|
(field.max_length is None or int(field.max_length) > 255)):
|
||||||
errors.append(
|
errors.append(
|
||||||
|
|
Loading…
Reference in New Issue