#36518: full_clean crashes on model with both a CheckConstraint and a
GeneratedField with a Case expression
-------------------------------------+-------------------------------------
Reporter: Olivier Dalang | Owner: Simon
| Charette
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Sarah Boyce):
Tried to look into this today a bit, note that the following is a possible
regression test:
{{{#!diff
--- a/tests/constraints/models.py
+++ b/tests/constraints/models.py
@@ -69,6 +69,29 @@ class GeneratedFieldVirtualProduct(models.Model):
required_db_features = {"supports_virtual_generated_columns"}
+class GeneratedFieldCaseWhenConstraint(models.Model):
+ age = models.IntegerField()
+ is_old_enough = models.GeneratedField(
+ expression=models.Case(
+ models.When(age__gte=18, then=models.Value(True)),
+ default=models.Value(False),
+ ),
+ db_persist=True,
+ output_field=models.BooleanField(),
+ )
+
+ class Meta:
+ required_db_features = {
+ "supports_stored_generated_columns",
+ "supports_table_check_constraints"
+ }
+ constraints = [
+ models.CheckConstraint(
+ name="age_valid", condition=models.Q(age__lt=100)
+ ),
+ ]
+
+
class UniqueConstraintProduct(models.Model):
name = models.CharField(max_length=255)
color = models.CharField(max_length=32, null=True)
diff --git a/tests/constraints/tests.py b/tests/constraints/tests.py
index bff8de8566..f13845c5d5 100644
--- a/tests/constraints/tests.py
+++ b/tests/constraints/tests.py
@@ -12,6 +12,7 @@ from django.test import SimpleTestCase, TestCase,
skipIfDBFeature, skipUnlessDBF
from .models import (
ChildModel,
ChildUniqueConstraintProduct,
+ GeneratedFieldCaseWhenConstraint,
GeneratedFieldStoredProduct,
GeneratedFieldVirtualProduct,
JSONFieldModel,
@@ -414,6 +415,15 @@ class CheckConstraintTests(TestCase):
constraint.validate(model, invalid_product, exclude={"price"})
constraint.validate(model, invalid_product, exclude={"rebate"})
+ @skipUnlessDBFeature(
+ "supports_stored_generated_columns",
"supports_table_check_constraints"
+ )
+ def test_full_clean_generated_field_case_when(self):
+ bob = GeneratedFieldCaseWhenConstraint(age=17)
+ bob.full_clean()
+ bob.save()
+ self.assertEqual(bob.is_old_enough, False)
+
def test_database_default(self):
models.CheckConstraint(
}}}
This test fails on main but passes once the current fix for #34871
(current PR
https://github.com/django/django/pull/19190) is applied
--
Ticket URL: <
https://code.djangoproject.com/ticket/36518#comment:5>