Răsfoiți Sursa

Fixed #31144 -- Relaxed system check for max_length of CharFields on MySQL/MariaDB by turning into a warning.

Rohit 5 ani în urmă
părinte
comite
2695ac8e04

+ 7 - 3
django/db/backends/mysql/validation.py

@@ -41,11 +41,15 @@ class DatabaseValidation(BaseDatabaseValidation):
         if (field_type.startswith('varchar') and field.unique and
                 (field.max_length is None or int(field.max_length) > 255)):
             errors.append(
-                checks.Error(
-                    '%s does not allow unique CharFields to have a max_length '
+                checks.Warning(
+                    '%s may not allow unique CharFields to have a max_length '
                     '> 255.' % self.connection.display_name,
                     obj=field,
-                    id='mysql.E001',
+                    hint=(
+                        'See: https://docs.djangoproject.com/en/%s/ref/'
+                        'databases/#mysql-character-fields' % get_docs_version()
+                    ),
+                    id='mysql.W003',
                 )
             )
 

+ 4 - 1
docs/ref/checks.txt

@@ -127,9 +127,12 @@ MySQL and MariaDB
 If you're using MySQL or MariaDB, the following checks will be performed:
 
 * **mysql.E001**: MySQL/MariaDB does not allow unique ``CharField``\s to have a
-  ``max_length`` > 255.
+  ``max_length`` > 255. *This check was changed to* ``mysql.W003`` *in Django
+  3.1 as the real maximum size depends on many factors.*
 * **mysql.W002**: MySQL/MariaDB Strict Mode is not set for database connection
   '<alias>'. See also :ref:`mysql-sql-mode`.
+* **mysql.W003**: MySQL/MariaDB may not allow unique ``CharField``\s to have a
+  ``max_length`` > 255.
 
 Model fields
 ------------

+ 7 - 2
docs/ref/databases.txt

@@ -581,13 +581,18 @@ these methods in no-op's based in the results of such detection.
 Notes on specific fields
 ------------------------
 
+.. _mysql-character-fields:
+
 Character fields
 ~~~~~~~~~~~~~~~~
 
-Any fields that are stored with ``VARCHAR`` column types have their
+Any fields that are stored with ``VARCHAR`` column types may have their
 ``max_length`` restricted to 255 characters if you are using ``unique=True``
 for the field. This affects :class:`~django.db.models.CharField`,
-:class:`~django.db.models.SlugField`.
+:class:`~django.db.models.SlugField`. See `the MySQL documentation`_ for more
+details.
+
+.. _the MySQL documentation: https://dev.mysql.com/doc/refman/en/create-index.html#create-index-column-prefixes
 
 ``TextField`` limitations
 ~~~~~~~~~~~~~~~~~~~~~~~~~

+ 8 - 3
tests/invalid_models_tests/test_ordinary_fields.py

@@ -8,6 +8,7 @@ from django.test.utils import isolate_apps, override_settings
 from django.utils.functional import lazy
 from django.utils.timezone import now
 from django.utils.translation import gettext_lazy as _
+from django.utils.version import get_docs_version
 
 
 @isolate_apps('invalid_models_tests')
@@ -372,11 +373,15 @@ class CharFieldTests(SimpleTestCase):
         field = Model._meta.get_field('field')
         validator = DatabaseValidation(connection=connection)
         self.assertEqual(validator.check_field(field), [
-            Error(
-                '%s does not allow unique CharFields to have a max_length > '
+            DjangoWarning(
+                '%s may not allow unique CharFields to have a max_length > '
                 '255.' % connection.display_name,
+                hint=(
+                    'See: https://docs.djangoproject.com/en/%s/ref/databases/'
+                    '#mysql-character-fields' % get_docs_version()
+                ),
                 obj=field,
-                id='mysql.E001',
+                id='mysql.W003',
             )
         ])