summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2020-03-18 13:27:23 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-03-18 18:41:40 +0100
commit5c8441a0b8787c14b45fb761550571baea48604e (patch)
tree54e3fd3029dd5c01758d5773e63b644bff6c440e
parentba4389a36b5fb1afce0cddb4e28233138b6612b7 (diff)
Refs #31055 -- Made long column names checks support databases aware.
-rw-r--r--django/db/models/base.py8
-rw-r--r--tests/invalid_models_tests/test_models.py13
2 files changed, 14 insertions, 7 deletions
diff --git a/django/db/models/base.py b/django/db/models/base.py
index 2555d8c1b5..b2a60629c9 100644
--- a/django/db/models/base.py
+++ b/django/db/models/base.py
@@ -1258,7 +1258,7 @@ class Model(metaclass=ModelBase):
errors += [
*cls._check_fields(**kwargs),
*cls._check_m2m_through_same_relationship(),
- *cls._check_long_column_names(),
+ *cls._check_long_column_names(databases),
]
clash_errors = (
*cls._check_id_field(),
@@ -1763,17 +1763,19 @@ class Model(metaclass=ModelBase):
return errors
@classmethod
- def _check_long_column_names(cls):
+ def _check_long_column_names(cls, databases):
"""
Check that any auto-generated column names are shorter than the limits
for each database in which the model will be created.
"""
+ if not databases:
+ return []
errors = []
allowed_len = None
db_alias = None
# Find the minimum max allowed length among all specified db_aliases.
- for db in settings.DATABASES:
+ for db in databases:
# skip databases where the model won't be created
if not router.allow_migrate_model(db, cls):
continue
diff --git a/tests/invalid_models_tests/test_models.py b/tests/invalid_models_tests/test_models.py
index 438c327003..9fd7058b20 100644
--- a/tests/invalid_models_tests/test_models.py
+++ b/tests/invalid_models_tests/test_models.py
@@ -1,6 +1,5 @@
import unittest
-from django.conf import settings
from django.core.checks import Error, Warning
from django.core.checks.model_checks import _check_lazy_references
from django.db import connection, connections, models
@@ -18,7 +17,7 @@ def get_max_column_name_length():
allowed_len = None
db_alias = None
- for db in settings.DATABASES:
+ for db in ('default', 'other'):
connection = connections[db]
max_name_length = connection.ops.max_name_length()
if max_name_length is not None and not connection.features.truncates_names:
@@ -408,7 +407,7 @@ class FieldNamesTests(SimpleTestCase):
db_column=long_field_name
).contribute_to_class(m2mcomplex, long_field_name)
- errors = ModelWithLongField.check()
+ errors = ModelWithLongField.check(databases=('default', 'other'))
# First error because of M2M field set on the model with long name.
m2m_long_name = "verylongmodelnamezzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz_id"
@@ -446,6 +445,9 @@ class FieldNamesTests(SimpleTestCase):
)
self.assertEqual(errors, expected)
+ # Check for long column names is called only for specified database
+ # aliases.
+ self.assertEqual(ModelWithLongField.check(databases=None), [])
@unittest.skipIf(max_column_name_length is None, "The database doesn't have a column name length limit.")
def test_local_field_long_column_name(self):
@@ -462,7 +464,7 @@ class FieldNamesTests(SimpleTestCase):
long_field_name2 = 'b' * (self.max_column_name_length + 1)
models.CharField(max_length=11).contribute_to_class(ModelWithLongField, long_field_name)
models.CharField(max_length=11, db_column='vlmn').contribute_to_class(ModelWithLongField, long_field_name2)
- self.assertEqual(ModelWithLongField.check(), [
+ self.assertEqual(ModelWithLongField.check(databases=('default', 'other')), [
Error(
'Autogenerated column name too long for field "%s". '
'Maximum length is "%s" for database "%s".'
@@ -472,6 +474,9 @@ class FieldNamesTests(SimpleTestCase):
id='models.E018',
)
])
+ # Check for long column names is called only for specified database
+ # aliases.
+ self.assertEqual(ModelWithLongField.check(databases=None), [])
def test_including_separator(self):
class Model(models.Model):