Ticket #2188: Option 2.diff

File Option 2.diff, 3.2 KB (added by Fraser Nevett <mail@…>, 18 years ago)

Triggers an error when the model is validated.

  • core/management.py

     
    809809    validates all models of all installed apps. Writes errors, if any, to outfile.
    810810    Returns number of errors.
    811811    """
    812     from django.db import models
     812    from django.db import models, connection
    813813    from django.db.models.fields.related import RelatedObject
     814    from django.conf import settings
    814815
    815816    e = ModelErrorCollection(outfile)
    816817    for cls in models.get_models(app):
     
    846847            if f.db_index not in (None, True, False):
    847848                e.add(opts, '"%s": "db_index" should be either None, True or False.' % f.name)
    848849
     850            # Check maxlength < 255 for older MySQL varchar fields
     851            if settings.DATABASE_ENGINE == 'mysql':
     852                db_version = connection.get_server_version()
     853                if db_version < (5, 0, 3):
     854                    if isinstance(f, models.CharField) and f.maxlength > 255:
     855                        e.add(opts, '"%s": CharFields cannot have a "maxlength" greater than 255 when you are using a version of MySQL older than 5.0.3; you are using %s.' % (f.name, '.'.join(str(n) for n in db_version[:3])))
     856                    if isinstance(f, models.CommaSeparatedIntegerField) and f.maxlength > 255:
     857                        e.add(opts, '"%s": CommaSeparatedIntegerField cannot have a "maxlength" greater than 255 when you are using a version of MySQL older than 5.0.3; you are using %s.' % (f.name, '.'.join(str(n) for n in db_version[:3])))
     858                    if isinstance(f, models.SlugField) and f.maxlength > 255:
     859                        e.add(opts, '"%s": SlugField cannot have a "maxlength" greater than 255 when you are using a version of MySQL older than 5.0.3; you are using %s.' % (f.name, '.'.join(str(n) for n in db_version[:3])))
     860
    849861            # Check to see if the related field will clash with any
    850862            # existing fields, m2m fields, m2m related objects or related objects
    851863            if f.rel:
  • db/backends/mysql/base.py

     
    1212    raise ImproperlyConfigured, "Error loading MySQLdb module: %s" % e
    1313from MySQLdb.converters import conversions
    1414from MySQLdb.constants import FIELD_TYPE
    15 import types
     15import types, re
    1616
    1717DatabaseError = Database.DatabaseError
    1818
     
    6161    def __init__(self):
    6262        self.connection = None
    6363        self.queries = []
     64        self.server_version = None
    6465
    6566    def _valid_connection(self):
    6667        if self.connection is not None:
     
    9596            return util.CursorDebugWrapper(MysqlDebugWrapper(cursor), self)
    9697        return cursor
    9798
     99    def get_server_version(self):
     100        if not self.server_version:
     101            if not self._valid_connection():
     102                self.cursor()
     103            version = re.compile('[\.-]').split(self.connection.get_server_info())
     104            self.server_version = tuple(int(x) for x in version[:3]) + tuple(version[3:])
     105        return self.server_version
     106
    98107    def _commit(self):
    99108        self.connection.commit()
    100109
Back to Top