You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

validation.py 1.3 KiB

1234567891011121314151617181920212223242526272829303132333435
  1. from django.core import checks
  2. from django.db.backends.base.validation 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, 'remote_field', None) is None:
  14. field_type = field.db_type(connection)
  15. # Ignore any non-concrete fields
  16. if field_type is None:
  17. return errors
  18. if (field_type.startswith('varchar') # Look for CharFields...
  19. and field.unique # ... that are unique
  20. and (field.max_length is None or int(field.max_length) > 255)):
  21. errors.append(
  22. checks.Error(
  23. ('MySQL does not allow unique CharFields to have a max_length > 255.'),
  24. hint=None,
  25. obj=field,
  26. id='mysql.E001',
  27. )
  28. )
  29. return errors