diff options
| author | Colleen Dunlap <colleendunlap@Colleens-Air.lan> | 2025-06-06 16:02:54 -0400 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2025-06-13 08:50:25 +0200 |
| commit | 830e69a868fa7713b6b690fa2e71e8a98f4639d8 (patch) | |
| tree | 62305bb261c11f6e04f17860a298723a23c9f646 | |
| parent | 3306b7283bc76a4e0cd647776fdc8b02d38d0935 (diff) | |
Fixed #36433 -- Fixed constraint validation crash when condition uses a ForeignKey attname.
Regression in e44e8327d3d88d86895735c0e427102063ff5b55.
Thank you to Jacob Walls for the report.
Co-authored-by: Simon Charette <charette.s@gmail.com>
| -rw-r--r-- | django/db/models/base.py | 1 | ||||
| -rw-r--r-- | tests/constraints/tests.py | 16 |
2 files changed, 17 insertions, 0 deletions
diff --git a/django/db/models/base.py b/django/db/models/base.py index d4559e0693..901743147d 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -1322,6 +1322,7 @@ class Model(AltersData, metaclass=ModelBase): if not value or not hasattr(value, "resolve_expression"): value = Value(value, field) field_map[field.name] = value + field_map[field.attname] = value if "pk" not in exclude: field_map["pk"] = Value(self.pk, meta.pk) if generated_fields: diff --git a/tests/constraints/tests.py b/tests/constraints/tests.py index 20a5357cc5..bff8de8566 100644 --- a/tests/constraints/tests.py +++ b/tests/constraints/tests.py @@ -361,6 +361,22 @@ class CheckConstraintTests(TestCase): constraint_with_pk.validate(ChildModel, ChildModel(id=1, age=1)) constraint_with_pk.validate(ChildModel, ChildModel(pk=1, age=1), exclude={"pk"}) + def test_validate_fk_attname(self): + constraint_with_fk = models.CheckConstraint( + condition=models.Q(uniqueconstraintproduct_ptr_id__isnull=False), + name="parent_ptr_present", + ) + with self.assertRaisesMessage( + ValidationError, "Constraint “parent_ptr_present” is violated." + ): + constraint_with_fk.validate( + ChildUniqueConstraintProduct, ChildUniqueConstraintProduct() + ) + constraint_with_fk.validate( + ChildUniqueConstraintProduct, + ChildUniqueConstraintProduct(uniqueconstraintproduct_ptr_id=1), + ) + @skipUnlessDBFeature("supports_json_field") def test_validate_jsonfield_exact(self): data = {"release": "5.0.2", "version": "stable"} |
