mirror of
https://github.com/django/django.git
synced 2025-08-04 10:59:45 +00:00
Fixed #36433 -- Fixed constraint validation crash when condition uses a ForeignKey attname.
Regression in e44e8327d3
.
Thank you to Jacob Walls for the report.
Co-authored-by: Simon Charette <charette.s@gmail.com>
This commit is contained in:
parent
3306b7283b
commit
830e69a868
2 changed files with 17 additions and 0 deletions
|
@ -1322,6 +1322,7 @@ class Model(AltersData, metaclass=ModelBase):
|
||||||
if not value or not hasattr(value, "resolve_expression"):
|
if not value or not hasattr(value, "resolve_expression"):
|
||||||
value = Value(value, field)
|
value = Value(value, field)
|
||||||
field_map[field.name] = value
|
field_map[field.name] = value
|
||||||
|
field_map[field.attname] = value
|
||||||
if "pk" not in exclude:
|
if "pk" not in exclude:
|
||||||
field_map["pk"] = Value(self.pk, meta.pk)
|
field_map["pk"] = Value(self.pk, meta.pk)
|
||||||
if generated_fields:
|
if generated_fields:
|
||||||
|
|
|
@ -361,6 +361,22 @@ class CheckConstraintTests(TestCase):
|
||||||
constraint_with_pk.validate(ChildModel, ChildModel(id=1, age=1))
|
constraint_with_pk.validate(ChildModel, ChildModel(id=1, age=1))
|
||||||
constraint_with_pk.validate(ChildModel, ChildModel(pk=1, age=1), exclude={"pk"})
|
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")
|
@skipUnlessDBFeature("supports_json_field")
|
||||||
def test_validate_jsonfield_exact(self):
|
def test_validate_jsonfield_exact(self):
|
||||||
data = {"release": "5.0.2", "version": "stable"}
|
data = {"release": "5.0.2", "version": "stable"}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue