3
3
class DatabaseValidation(BaseDatabaseValidation):
4
4
def validate_field(self, errors, opts, f):
6
There are some field length restrictions for MySQL:
8
- Prior to version 5.0.3, character fields could not exceed 255
10
- No character (varchar) fields can have a length exceeding 255
11
characters if they have a unique index on them.
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.
13
10
from django.db import models
14
db_version = self.connection.get_server_version()
15
11
varchar_fields = (models.CharField, models.CommaSeparatedIntegerField,
17
if isinstance(f, varchar_fields) and f.max_length > 255:
18
if db_version < (5, 0, 3):
19
msg = '"%(name)s": %(cls)s cannot have a "max_length" greater than 255 when you are using a version of MySQL prior to 5.0.3 (you are using %(version)s).'
20
elif f.unique == True:
21
msg = '"%(name)s": %(cls)s cannot have a "max_length" greater than 255 when using "unique=True".'
26
errors.add(opts, msg % {'name': f.name, 'cls': f.__class__.__name__, 'version': '.'.join([str(n) for n in db_version[:3]])})
13
if isinstance(f, varchar_fields) and f.max_length > 255 and f.unique:
14
msg = '"%(name)s": %(cls)s cannot have a "max_length" greater than 255 when using "unique=True".'
15
errors.add(opts, msg % {'name': f.name, 'cls': f.__class__.__name__})