2008-08-11 20:11:25 +08:00
|
|
|
from django.db.backends import BaseDatabaseValidation
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
class DatabaseValidation(BaseDatabaseValidation):
|
|
|
|
def validate_field(self, errors, opts, f):
|
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
|
|
|
"""
|
2008-08-11 20:11:25 +08:00
|
|
|
from django.db import models
|
2008-12-16 14:43:18 +08:00
|
|
|
varchar_fields = (models.CharField, models.CommaSeparatedIntegerField,
|
|
|
|
models.SlugField)
|
2013-05-06 01:44:43 +08:00
|
|
|
if (isinstance(f, varchar_fields) and f.unique
|
|
|
|
and (f.max_length is None or int(f.max_length) > 255)):
|
2012-04-21 11:04:10 +08:00
|
|
|
msg = '"%(name)s": %(cls)s cannot have a "max_length" greater than 255 when using "unique=True".'
|
|
|
|
errors.add(opts, msg % {'name': f.name, 'cls': f.__class__.__name__})
|