diff --git a/django/db/backends/mysql/validation.py b/django/db/backends/mysql/validation.py index 5a50663dd2..3492034eba 100644 --- a/django/db/backends/mysql/validation.py +++ b/django/db/backends/mysql/validation.py @@ -32,25 +32,33 @@ class DatabaseValidation(BaseDatabaseValidation): No character (varchar) fields can have a length exceeding 255 characters if they have a unique index on them. """ - from django.db import connection - errors = super(DatabaseValidation, self).check_field(field, **kwargs) # Ignore any related fields. - if getattr(field, 'remote_field', None) is None: - field_type = field.db_type(connection) + if getattr(field, 'remote_field', None): + return errors - # Ignore any non-concrete fields - if field_type is 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 - if (field_type.startswith('varchar') and field.unique and - (field.max_length is None or int(field.max_length) > 255)): - errors.append( - checks.Error( - 'MySQL does not allow unique CharFields to have a max_length > 255.', - obj=field, - id='mysql.E001', - ) + 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 + (field.max_length is None or int(field.max_length) > 255)): + errors.append( + checks.Error( + 'MySQL does not allow unique CharFields to have a max_length > 255.', + obj=field, + id='mysql.E001', ) + ) return errors diff --git a/tests/invalid_models_tests/test_ordinary_fields.py b/tests/invalid_models_tests/test_ordinary_fields.py index 4d4fd3039f..3f601e1fcf 100644 --- a/tests/invalid_models_tests/test_ordinary_fields.py +++ b/tests/invalid_models_tests/test_ordinary_fields.py @@ -217,7 +217,7 @@ class CharFieldTests(TestCase): field = models.CharField(unique=True, max_length=256) field = Model._meta.get_field('field') - validator = DatabaseValidation(connection=None) + validator = DatabaseValidation(connection=connection) errors = validator.check_field(field) expected = [ Error(