diff options
| author | Adam Johnson <me@adamj.eu> | 2025-11-12 19:47:16 +0000 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2025-11-13 09:16:46 +0100 |
| commit | abfa4619fb818ff694c22e962a280673e085239e (patch) | |
| tree | 85fcaee04e89b0ad9b9334f4909c6e661aff2019 /tests/model_forms | |
| parent | 5401b125abca53200eacb62c8a10e602359b76d4 (diff) | |
Fixed #36730 -- Fixed constraint validation crash for excluded FK attnames.
Regression in e44e8327d3d88d86895735c0e427102063ff5b55.
Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com>
Diffstat (limited to 'tests/model_forms')
| -rw-r--r-- | tests/model_forms/models.py | 19 | ||||
| -rw-r--r-- | tests/model_forms/tests.py | 15 |
2 files changed, 34 insertions, 0 deletions
diff --git a/tests/model_forms/models.py b/tests/model_forms/models.py index f6c34a3521..83daa13d71 100644 --- a/tests/model_forms/models.py +++ b/tests/model_forms/models.py @@ -543,3 +543,22 @@ class ConstraintsModel(models.Model): violation_error_message="Price must be greater than zero.", ), ] + + +class AttnameConstraintsModel(models.Model): + left = models.ForeignKey( + "self", related_name="+", null=True, on_delete=models.SET_NULL + ) + right = models.ForeignKey( + "self", related_name="+", null=True, on_delete=models.SET_NULL + ) + + class Meta: + required_db_features = {"supports_table_check_constraints"} + constraints = [ + models.CheckConstraint( + name="%(app_label)s_%(class)s_left_not_right", + # right_id here is the ForeignKey's attname, not name. + condition=~models.Q(left=models.F("right_id")), + ), + ] diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py index f0334e1e86..129ce56c7a 100644 --- a/tests/model_forms/tests.py +++ b/tests/model_forms/tests.py @@ -30,6 +30,7 @@ from django.utils.version import PY314, PYPY from .models import ( Article, ArticleStatus, + AttnameConstraintsModel, Author, Author1, Award, @@ -3766,3 +3767,17 @@ class ConstraintValidationTests(TestCase): self.assertEqual( full_form.errors, {"__all__": ["Price must be greater than zero."]} ) + + def test_check_constraint_refs_excluded_field_attname(self): + left = AttnameConstraintsModel.objects.create() + instance = AttnameConstraintsModel.objects.create(left=left) + data = { + "left": str(left.id), + "right": "", + } + AttnameConstraintsModelForm = modelform_factory( + AttnameConstraintsModel, fields="__all__" + ) + full_form = AttnameConstraintsModelForm(data, instance=instance) + self.assertFalse(full_form.is_valid()) + self.assertEqual(full_form.errors, {"right": ["This field is required."]}) |
