diff options
| author | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2008-12-16 06:43:18 +0000 |
|---|---|---|
| committer | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2008-12-16 06:43:18 +0000 |
| commit | b58a260c56c5d05d71ac1073bc05eda8eca0ea5f (patch) | |
| tree | e5b97f4febeff7778807893e6025aced59ff7c50 /django | |
| parent | fddc0c589e03c1e5eb581aaa947a39720163d7d5 (diff) | |
Fixed #9431 -- Added extra validation for VARCHAR-based fields on MySQL.
max_length > 255 and unique=True is not permitted. Based on a patch from
adamnelson.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@9650 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
| -rw-r--r-- | django/db/backends/mysql/validation.py | 27 |
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]])}) + |
