diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py
|
a
|
b
|
|
| 15 | 15 | from django.core.exceptions import ImproperlyConfigured |
| 16 | 16 | raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e) |
| 17 | 17 | |
| | 18 | from django.utils.functional import cached_property |
| | 19 | |
| 18 | 20 | # We want version (1, 2, 1, 'final', 2) or later. We can't just use |
| 19 | 21 | # lexicographic ordering in this check because then (1, 2, 1, 'gamma') |
| 20 | 22 | # inadvertently passes the version test. |
| 21 | 23 | version = Database.version_info |
| 22 | | if (version < (1,2,1) or (version[:3] == (1, 2, 1) and |
| | 24 | if (version < (1, 2, 1) or (version[:3] == (1, 2, 1) and |
| 23 | 25 | (len(version) < 5 or version[3] != 'final' or version[4] < 2))): |
| 24 | 26 | from django.core.exceptions import ImproperlyConfigured |
| 25 | 27 | raise ImproperlyConfigured("MySQLdb-1.2.1p2 or newer is required; you have %s" % Database.__version__) |
| … |
… |
|
| 163 | 165 | supports_timezones = False |
| 164 | 166 | requires_explicit_null_ordering_when_grouping = True |
| 165 | 167 | allows_primary_key_0 = False |
| | 168 | uses_savepoints = True |
| 166 | 169 | |
| 167 | 170 | def __init__(self, connection): |
| 168 | 171 | super(DatabaseFeatures, self).__init__(connection) |
| … |
… |
|
| 387 | 390 | self.connection = Database.connect(**kwargs) |
| 388 | 391 | self.connection.encoders[SafeUnicode] = self.connection.encoders[unicode] |
| 389 | 392 | self.connection.encoders[SafeString] = self.connection.encoders[str] |
| 390 | | self.features.uses_savepoints = \ |
| 391 | | self.get_server_version() >= (5, 0, 3) |
| 392 | 393 | connection_created.send(sender=self.__class__, connection=self) |
| 393 | 394 | cursor = self.connection.cursor() |
| 394 | 395 | if new_connection: |
| … |
… |
|
| 405 | 406 | except Database.NotSupportedError: |
| 406 | 407 | pass |
| 407 | 408 | |
| 408 | | def get_server_version(self): |
| | 409 | @cached_property |
| | 410 | def mysql_version(self): |
| 409 | 411 | if not self.server_version: |
| 410 | 412 | if not self._valid_connection(): |
| 411 | | self.cursor() |
| | 413 | self.cursor().close() |
| 412 | 414 | m = server_version_re.match(self.connection.get_server_info()) |
| 413 | 415 | if not m: |
| 414 | 416 | raise Exception('Unable to determine MySQL version from version string %r' % self.connection.get_server_info()) |
diff --git a/django/db/backends/mysql/validation.py b/django/db/backends/mysql/validation.py
|
a
|
b
|
|
| 3 | 3 | class DatabaseValidation(BaseDatabaseValidation): |
| 4 | 4 | def validate_field(self, errors, opts, f): |
| 5 | 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. |
| | 6 | MySQL has the following field length restriction: |
| | 7 | No character (varchar) fields can have a length exceeding 255 |
| | 8 | characters if they have a unique index on them. |
| 12 | 9 | """ |
| 13 | 10 | from django.db import models |
| 14 | | from MySQLdb import OperationalError |
| 15 | | try: |
| 16 | | db_version = self.connection.get_server_version() |
| 17 | | text_version = '.'.join([str(n) for n in db_version[:3]]) |
| 18 | | except OperationalError: |
| 19 | | db_version = None |
| 20 | | text_version = '' |
| 21 | 11 | varchar_fields = (models.CharField, models.CommaSeparatedIntegerField, |
| 22 | 12 | models.SlugField) |
| 23 | 13 | if isinstance(f, varchar_fields) and f.max_length > 255: |
| 24 | | if db_version and db_version < (5, 0, 3): |
| 25 | | 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).' |
| 26 | | elif f.unique == True: |
| | 14 | if f.unique == True: |
| 27 | 15 | msg = '"%(name)s": %(cls)s cannot have a "max_length" greater than 255 when using "unique=True".' |
| 28 | 16 | else: |
| 29 | 17 | msg = None |
| 30 | 18 | |
| 31 | 19 | if msg: |
| 32 | | errors.add(opts, msg % {'name': f.name, 'cls': f.__class__.__name__, 'version': text_version}) |
| | 20 | errors.add(opts, msg % {'name': f.name, 'cls': f.__class__.__name__}) |
diff --git a/docs/ref/databases.txt b/docs/ref/databases.txt
|
a
|
b
|
|
| 122 | 122 | MySQL notes |
| 123 | 123 | =========== |
| 124 | 124 | |
| 125 | | Django expects the database to support transactions, referential integrity, and |
| 126 | | Unicode (UTF-8 encoding). Fortunately, MySQL_ has all these features as |
| 127 | | available as far back as 3.23. While it may be possible to use 3.23 or 4.0, |
| 128 | | you'll probably have less trouble if you use 4.1 or 5.0. |
| | 125 | Version support |
| | 126 | --------------- |
| 129 | 127 | |
| 130 | | MySQL 4.1 |
| 131 | | --------- |
| 132 | | |
| 133 | | `MySQL 4.1`_ has greatly improved support for character sets. It is possible to |
| 134 | | set different default character sets on the database, table, and column. |
| 135 | | Previous versions have only a server-wide character set setting. It's also the |
| 136 | | first version where the character set can be changed on the fly. 4.1 also has |
| 137 | | support for views, but Django currently doesn't use views. |
| 138 | | |
| 139 | | MySQL 5.0 |
| 140 | | --------- |
| | 128 | Django supports MySQL 5.0.3 and higher. |
| 141 | 129 | |
| 142 | 130 | `MySQL 5.0`_ adds the ``information_schema`` database, which contains detailed |
| 143 | 131 | data on all database schema. Django's ``inspectdb`` feature uses this |
| 144 | | ``information_schema`` if it's available. 5.0 also has support for stored |
| 145 | | procedures, but Django currently doesn't use stored procedures. |
| | 132 | ``information_schema`` if it's available. |
| | 133 | |
| | 134 | .. versionchanged:: 1.5 |
| | 135 | The minimum version requirement of MySQL 5.0.3 was set in Django 1.5. |
| | 136 | |
| | 137 | Django expects the database to support transactions, referential integrity, and |
| | 138 | Unicode (UTF-8 encoding). |
| 146 | 139 | |
| 147 | 140 | .. _MySQL: http://www.mysql.com/ |
| 148 | | .. _MySQL 4.1: http://dev.mysql.com/doc/refman/4.1/en/index.html |
| 149 | 141 | .. _MySQL 5.0: http://dev.mysql.com/doc/refman/5.0/en/index.html |
| 150 | 142 | |
| 151 | 143 | Storage engines |
| … |
… |
|
| 381 | 373 | :class:`~django.db.models.SlugField` and |
| 382 | 374 | :class:`~django.db.models.CommaSeparatedIntegerField`. |
| 383 | 375 | |
| 384 | | Furthermore, if you are using a version of MySQL prior to 5.0.3, all of those |
| 385 | | column types have a maximum length restriction of 255 characters, regardless |
| 386 | | of whether ``unique=True`` is specified or not. |
| 387 | | |
| 388 | 376 | DateTime fields |
| 389 | 377 | ~~~~~~~~~~~~~~~ |
| 390 | 378 | |