From 271fdab8b78af558238df51c64b4d1c8dd0792bb Mon Sep 17 00:00:00 2001 From: Taoup Date: Wed, 19 Feb 2020 19:14:54 +0800 Subject: [PATCH] Fixed #31286 -- Made database specific fields checks databases aware. Follow up to 0b83c8cc4db95812f1e15ca19d78614e94cf38dd. --- django/db/models/fields/__init__.py | 13 ++++++++----- tests/check_framework/test_multi_db.py | 4 ++-- tests/invalid_models_tests/test_backend_specific.py | 2 +- tests/invalid_models_tests/test_ordinary_fields.py | 2 +- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 6bd95f542c..caa03665ea 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -335,12 +335,15 @@ class Field(RegisterLookupMixin): else: return [] - def _check_backend_specific_checks(self, **kwargs): + def _check_backend_specific_checks(self, databases=None, **kwargs): + if databases is None: + return [] app_label = self.model._meta.app_label - for db in connections: - if router.allow_migrate(db, app_label, model_name=self.model._meta.model_name): - return connections[db].validation.check_field(self, **kwargs) - return [] + errors = [] + for alias in databases: + if router.allow_migrate(alias, app_label, model_name=self.model._meta.model_name): + errors.extend(connections[alias].validation.check_field(self, **kwargs)) + return errors def _check_validators(self): errors = [] diff --git a/tests/check_framework/test_multi_db.py b/tests/check_framework/test_multi_db.py index f51d28a215..0697b3b94a 100644 --- a/tests/check_framework/test_multi_db.py +++ b/tests/check_framework/test_multi_db.py @@ -27,7 +27,7 @@ class TestMultiDBChecks(SimpleTestCase): model = Model() with self._patch_check_field_on('default') as mock_check_field_default: with self._patch_check_field_on('other') as mock_check_field_other: - model.check() + model.check(databases={'default', 'other'}) self.assertTrue(mock_check_field_default.called) self.assertFalse(mock_check_field_other.called) @@ -38,6 +38,6 @@ class TestMultiDBChecks(SimpleTestCase): model = OtherModel() with self._patch_check_field_on('other') as mock_check_field_other: with self._patch_check_field_on('default') as mock_check_field_default: - model.check() + model.check(databases={'default', 'other'}) self.assertTrue(mock_check_field_other.called) self.assertFalse(mock_check_field_default.called) diff --git a/tests/invalid_models_tests/test_backend_specific.py b/tests/invalid_models_tests/test_backend_specific.py index 70c7d0b05a..236909d50e 100644 --- a/tests/invalid_models_tests/test_backend_specific.py +++ b/tests/invalid_models_tests/test_backend_specific.py @@ -25,4 +25,4 @@ class BackendSpecificChecksTests(SimpleTestCase): field = Model._meta.get_field('field') with mock.patch.object(connections['default'].validation, 'check_field', return_value=[error]): - self.assertEqual(field.check(), [error]) + self.assertEqual(field.check(databases={'default'}), [error]) diff --git a/tests/invalid_models_tests/test_ordinary_fields.py b/tests/invalid_models_tests/test_ordinary_fields.py index ce7a67ca51..c8b7e69634 100644 --- a/tests/invalid_models_tests/test_ordinary_fields.py +++ b/tests/invalid_models_tests/test_ordinary_fields.py @@ -758,7 +758,7 @@ class TextFieldTests(TestCase): value = models.TextField(db_index=True) field = Model._meta.get_field('value') field_type = field.db_type(connection) - self.assertEqual(field.check(), [ + self.assertEqual(field.check(databases=self.databases), [ DjangoWarning( '%s does not support a database index on %s columns.' % (connection.display_name, field_type),