web/lib/django/db/backends/mysql/validation.py
changeset 38 77b6da96e6f1
equal deleted inserted replaced
37:8d941af65caf 38:77b6da96e6f1
       
     1 from django.db.backends import BaseDatabaseValidation
       
     2 
       
     3 class DatabaseValidation(BaseDatabaseValidation):
       
     4     def validate_field(self, errors, opts, f):
       
     5         """
       
     6         There are some field length restrictions for MySQL:
       
     7 
       
     8         - Prior to version 5.0.3, character fields could not exceed 255
       
     9           characters in length.
       
    10         - No character (varchar) fields can have a length exceeding 255
       
    11           characters if they have a unique index on them.
       
    12         """
       
    13         from django.db import models
       
    14         db_version = self.connection.get_server_version()
       
    15         varchar_fields = (models.CharField, models.CommaSeparatedIntegerField,
       
    16                 models.SlugField)
       
    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".'
       
    22             else:
       
    23                 msg = None
       
    24 
       
    25             if msg:
       
    26                 errors.add(opts, msg % {'name': f.name, 'cls': f.__class__.__name__, 'version': '.'.join([str(n) for n in db_version[:3]])})