Fixed #31055 -- Made constraint checks support databases aware.

This commit is contained in:
Simon Charette 2020-01-31 01:38:48 -05:00 committed by Mariusz Felisiak
parent 430e796980
commit 71756bdfed
2 changed files with 8 additions and 7 deletions

View File

@ -1255,6 +1255,7 @@ class Model(metaclass=ModelBase):
def check(cls, **kwargs): def check(cls, **kwargs):
errors = [*cls._check_swappable(), *cls._check_model(), *cls._check_managers(**kwargs)] errors = [*cls._check_swappable(), *cls._check_model(), *cls._check_managers(**kwargs)]
if not cls._meta.swapped: if not cls._meta.swapped:
databases = kwargs.get('databases') or []
errors += [ errors += [
*cls._check_fields(**kwargs), *cls._check_fields(**kwargs),
*cls._check_m2m_through_same_relationship(), *cls._check_m2m_through_same_relationship(),
@ -1277,7 +1278,7 @@ class Model(metaclass=ModelBase):
*cls._check_unique_together(), *cls._check_unique_together(),
*cls._check_indexes(), *cls._check_indexes(),
*cls._check_ordering(), *cls._check_ordering(),
*cls._check_constraints(), *cls._check_constraints(databases),
] ]
return errors return errors
@ -1836,9 +1837,9 @@ class Model(metaclass=ModelBase):
return errors return errors
@classmethod @classmethod
def _check_constraints(cls): def _check_constraints(cls, databases):
errors = [] errors = []
for db in settings.DATABASES: for db in databases:
if not router.allow_migrate_model(db, cls): if not router.allow_migrate_model(db, cls):
continue continue
connection = connections[db] connection = connections[db]

View File

@ -6,7 +6,7 @@ from django.core.checks.model_checks import _check_lazy_references
from django.db import connection, connections, models from django.db import connection, connections, models
from django.db.models.functions import Lower from django.db.models.functions import Lower
from django.db.models.signals import post_init from django.db.models.signals import post_init
from django.test import SimpleTestCase from django.test import SimpleTestCase, TestCase
from django.test.utils import isolate_apps, override_settings, register_lookup from django.test.utils import isolate_apps, override_settings, register_lookup
@ -1212,7 +1212,7 @@ class OtherModelTests(SimpleTestCase):
@isolate_apps('invalid_models_tests') @isolate_apps('invalid_models_tests')
class ConstraintsTests(SimpleTestCase): class ConstraintsTests(TestCase):
def test_check_constraints(self): def test_check_constraints(self):
class Model(models.Model): class Model(models.Model):
age = models.IntegerField() age = models.IntegerField()
@ -1220,7 +1220,7 @@ class ConstraintsTests(SimpleTestCase):
class Meta: class Meta:
constraints = [models.CheckConstraint(check=models.Q(age__gte=18), name='is_adult')] constraints = [models.CheckConstraint(check=models.Q(age__gte=18), name='is_adult')]
errors = Model.check() errors = Model.check(databases=self.databases)
warn = Warning( warn = Warning(
'%s does not support check constraints.' % connection.display_name, '%s does not support check constraints.' % connection.display_name,
hint=( hint=(
@ -1230,7 +1230,7 @@ class ConstraintsTests(SimpleTestCase):
obj=Model, obj=Model,
id='models.W027', id='models.W027',
) )
expected = [] if connection.features.supports_table_check_constraints else [warn, warn] expected = [] if connection.features.supports_table_check_constraints else [warn]
self.assertCountEqual(errors, expected) self.assertCountEqual(errors, expected)
def test_check_constraints_required_db_features(self): def test_check_constraints_required_db_features(self):