validation.py 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. from django.core import checks
  2. from django.db.backends import BaseDatabaseValidation
  3. class DatabaseValidation(BaseDatabaseValidation):
  4. def check_field(self, field, **kwargs):
  5. """
  6. MySQL has the following field length restriction:
  7. No character (varchar) fields can have a length exceeding 255
  8. characters if they have a unique index on them.
  9. """
  10. from django.db import connection
  11. errors = super(DatabaseValidation, self).check_field(field, **kwargs)
  12. # Ignore any related fields.
  13. if getattr(field, 'rel', None) is None:
  14. field_type = field.db_type(connection)
  15. if (field_type.startswith('varchar') # Look for CharFields...
  16. and field.unique # ... that are unique
  17. and (field.max_length is None or int(field.max_length) > 255)):
  18. errors.append(
  19. checks.Error(
  20. ('MySQL does not allow unique CharFields to have a max_length > 255.'),
  21. hint=None,
  22. obj=field,
  23. id='mysql.E001',
  24. )
  25. )
  26. return errors