2014-01-20 10:45:21 +08:00
|
|
|
from django.core import checks
|
2015-01-13 04:20:40 +08:00
|
|
|
from django.db.backends.base.validation import BaseDatabaseValidation
|
2008-08-11 20:11:25 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
class DatabaseValidation(BaseDatabaseValidation):
|
2014-01-20 10:45:21 +08:00
|
|
|
def check_field(self, field, **kwargs):
|
2008-12-16 14:43:18 +08:00
|
|
|
"""
|
2012-04-21 11:04:10 +08:00
|
|
|
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.
|
2008-12-16 14:43:18 +08:00
|
|
|
"""
|
2014-01-20 10:45:21 +08:00
|
|
|
from django.db import connection
|
|
|
|
|
|
|
|
errors = super(DatabaseValidation, self).check_field(field, **kwargs)
|
2014-01-21 18:37:49 +08:00
|
|
|
|
|
|
|
# Ignore any related fields.
|
2015-02-26 22:19:17 +08:00
|
|
|
if getattr(field, 'remote_field', None) is None:
|
2014-01-20 10:45:21 +08:00
|
|
|
field_type = field.db_type(connection)
|
|
|
|
|
2014-08-12 20:08:40 +08:00
|
|
|
# Ignore any non-concrete fields
|
|
|
|
if field_type is None:
|
|
|
|
return errors
|
|
|
|
|
2014-05-19 21:57:06 +08:00
|
|
|
if (field_type.startswith('varchar') # Look for CharFields...
|
2014-01-21 18:37:49 +08:00
|
|
|
and field.unique # ... that are unique
|
|
|
|
and (field.max_length is None or int(field.max_length) > 255)):
|
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
2014-03-03 14:31:55 +08:00
|
|
|
('MySQL does not allow unique CharFields to have a max_length > 255.'),
|
2014-01-21 18:37:49 +08:00
|
|
|
hint=None,
|
|
|
|
obj=field,
|
2014-03-03 14:31:55 +08:00
|
|
|
id='mysql.E001',
|
2014-01-21 18:37:49 +08:00
|
|
|
)
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
return errors
|