Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #2188 -- Raise an error when using long CharFields in combinati…

…on with

older MySQL versions. Thanks, Fraser Nevett <mail@nevett.org> .


git-svn-id: http://code.djangoproject.com/svn/django/trunk@3855 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 4e76727fec2fa898314b4525a39ce995692f2a78 1 parent 5371ee1
Malcolm Tredinnick authored September 26, 2006
1  AUTHORS
@@ -119,6 +119,7 @@ answer newbie questions, and generally made Django that much better:
119 119
     Eric Moritz <http://eric.themoritzfamily.com/>
120 120
     Robin Munn <http://www.geekforgod.com/>
121 121
     Nebojša Dorđević
  122
+    Fraser Nevett <mail@nevett.org>
122 123
     Sam Newman <http://www.magpiebrain.com/>
123 124
     Neal Norwitz <nnorwitz@google.com>
124 125
     oggie rob <oz.robharvey@gmail.com>
9  django/core/management.py
@@ -820,7 +820,8 @@ def get_validation_errors(outfile, app=None):
820 820
     validates all models of all installed apps. Writes errors, if any, to outfile.
821 821
     Returns number of errors.
822 822
     """
823  
-    from django.db import models
  823
+    from django.conf import settings
  824
+    from django.db import models, connection
824 825
     from django.db.models.loading import get_app_errors
825 826
     from django.db.models.fields.related import RelatedObject
826 827
 
@@ -862,6 +863,12 @@ def get_validation_errors(outfile, app=None):
862 863
             if f.db_index not in (None, True, False):
863 864
                 e.add(opts, '"%s": "db_index" should be either None, True or False.' % f.name)
864 865
 
  866
+            # Check that maxlength <= 255 if using older MySQL versions.
  867
+            if settings.DATABASE_ENGINE == 'mysql':
  868
+                db_version = connection.get_server_version()
  869
+                if db_version < (5, 0, 3) and isinstance(f, (models.CharField, models.CommaSeparatedIntegerField, models.SlugField)) and f.maxlength > 255:
  870
+                    e.add(opts, '"%s": %s cannot have a "maxlength" 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])))
  871
+
865 872
             # Check to see if the related field will clash with any
866 873
             # existing fields, m2m fields, m2m related objects or related objects
867 874
             if f.rel:
12  django/db/backends/mysql/base.py
@@ -13,6 +13,7 @@
13 13
 from MySQLdb.converters import conversions
14 14
 from MySQLdb.constants import FIELD_TYPE
15 15
 import types
  16
+import re
16 17
 
17 18
 DatabaseError = Database.DatabaseError
18 19
 
@@ -24,6 +25,8 @@
24 25
     FIELD_TYPE.TIME: util.typecast_time,
25 26
 })
26 27
 
  28
+server_version_re = re.compile('[.-]')
  29
+
27 30
 # This is an extra debug layer over MySQL queries, to display warnings.
28 31
 # It's only used when DEBUG=True.
29 32
 class MysqlDebugWrapper:
@@ -61,6 +64,7 @@ class DatabaseWrapper(local):
61 64
     def __init__(self):
62 65
         self.connection = None
63 66
         self.queries = []
  67
+        self.server_version = None
64 68
 
65 69
     def _valid_connection(self):
66 70
         if self.connection is not None:
@@ -110,6 +114,14 @@ def close(self):
110 114
             self.connection.close()
111 115
             self.connection = None
112 116
 
  117
+    def get_server_version(self):
  118
+        if not self.server_version:
  119
+            if not self._valid_connection():
  120
+                self.cursor()
  121
+            version = server_version_re.split(self.connection.get_server_info())
  122
+            self.server_version = tuple([int(x) for x in version[:3]]) + tuple(version[3:])
  123
+        return self.server_version
  124
+
113 125
 supports_constraints = True
114 126
 
115 127
 def quote_name(name):

0 notes on commit 4e76727

Please sign in to comment.
Something went wrong with that request. Please try again.