1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #30581 -- Added support for Meta.constraints validation.

Thanks Simon Charette, Keryn Knight, and Mariusz Felisiak for reviews.
This commit is contained in:
Gagaro
2022-01-31 16:04:13 +01:00
committed by Mariusz Felisiak
parent 441103a04d
commit 667105877e
17 changed files with 852 additions and 88 deletions

View File

@@ -2198,6 +2198,66 @@ class ConstraintsTests(TestCase):
]
self.assertCountEqual(errors, expected_errors)
def test_check_constraint_raw_sql_check(self):
class Model(models.Model):
class Meta:
required_db_features = {"supports_table_check_constraints"}
constraints = [
models.CheckConstraint(check=models.Q(id__gt=0), name="q_check"),
models.CheckConstraint(
check=models.ExpressionWrapper(
models.Q(price__gt=20),
output_field=models.BooleanField(),
),
name="expression_wrapper_check",
),
models.CheckConstraint(
check=models.expressions.RawSQL(
"id = 0",
params=(),
output_field=models.BooleanField(),
),
name="raw_sql_check",
),
models.CheckConstraint(
check=models.Q(
models.ExpressionWrapper(
models.Q(
models.expressions.RawSQL(
"id = 0",
params=(),
output_field=models.BooleanField(),
)
),
output_field=models.BooleanField(),
)
),
name="nested_raw_sql_check",
),
]
expected_warnings = (
[
Warning(
"Check constraint 'raw_sql_check' contains RawSQL() expression and "
"won't be validated during the model full_clean().",
hint="Silence this warning if you don't care about it.",
obj=Model,
id="models.W045",
),
Warning(
"Check constraint 'nested_raw_sql_check' contains RawSQL() "
"expression and won't be validated during the model full_clean().",
hint="Silence this warning if you don't care about it.",
obj=Model,
id="models.W045",
),
]
if connection.features.supports_table_check_constraints
else []
)
self.assertEqual(Model.check(databases=self.databases), expected_warnings)
def test_unique_constraint_with_condition(self):
class Model(models.Model):
age = models.IntegerField()