summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
Diffstat (limited to 'django')
-rw-r--r--django/db/backends/mysql/validation.py27
1 files changed, 21 insertions, 6 deletions
diff --git a/django/db/backends/mysql/validation.py b/django/db/backends/mysql/validation.py
index 85354a8468..3014d7bae5 100644
--- a/django/db/backends/mysql/validation.py
+++ b/django/db/backends/mysql/validation.py
@@ -2,12 +2,27 @@ from django.db.backends import BaseDatabaseValidation
class DatabaseValidation(BaseDatabaseValidation):
def validate_field(self, errors, opts, f):
- "Prior to MySQL 5.0.3, character fields could not exceed 255 characters"
+ """
+ There are some field length restrictions for MySQL:
+
+ - Prior to version 5.0.3, character fields could not exceed 255
+ characters in length.
+ - No character (varchar) fields can have a length exceeding 255
+ characters if they have a unique index on them.
+ """
from django.db import models
from django.db import connection
db_version = connection.get_server_version()
- if db_version < (5, 0, 3) and isinstance(f, (models.CharField, models.CommaSeparatedIntegerField, models.SlugField)) and f.max_length > 255:
- errors.add(opts,
- '"%s": %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 %s).' %
- (f.name, f.__class__.__name__, '.'.join([str(n) for n in db_version[:3]])))
- \ No newline at end of file
+ varchar_fields = (models.CharField, models.CommaSeparatedIntegerField,
+ models.SlugField)
+ if isinstance(f, varchar_fields) and f.max_length > 255:
+ if db_version < (5, 0, 3):
+ 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).'
+ if f.unique == True:
+ msg = '"%(name)s": %(cls)s cannot have a "max_length" greater than 255 when using "unique=True".'
+ else:
+ msg = None
+
+ if msg:
+ errors.add(opts, msg % {'name': f.name, 'cls': f.__class__.__name__, 'version': '.'.join([str(n) for n in db_version[:3]])})
+