#36433: Model validation of constraints fails if condition's Q object references
foreign key by _id
-------------------------------------+-------------------------------------
     Reporter:  Jacob Walls          |                    Owner:  (none)
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  dev
  (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
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

 * stage:  Unreviewed => Accepted

Comment:

 Thanks for the report Jacob!

 I think this is a bug in `_get_field_expression_map`, it should augment
 the map with `Field.attname` as well

 {{{#!diff
 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 @@ def _get_field_expression_map(self, meta,
 exclude=None):
              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..2816f01150 100644
 --- a/tests/constraints/tests.py
 +++ b/tests/constraints/tests.py
 @@ -361,6 +361,16 @@ def test_validate_pk_field(self):
              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_pk = models.CheckConstraint(
 +
 condition=models.Q(uniqueconstraintproduct_ptr_id__isnull=False),
 +            name="parent_ptr_present",
 +        )
 +        with self.assertRaises(ValidationError):
 +            constraint_with_pk.validate(
 +                ChildUniqueConstraintProduct,
 ChildUniqueConstraintProduct()
 +            )
 +
      @skipUnlessDBFeature("supports_json_field")
      def test_validate_jsonfield_exact(self):
          data = {"release": "5.0.2", "version": "stable"}
 }}}

 As for the unhelpful error message I wonder if we should make `Q.check`
 catch `FieldError` and re-reraise with `against` keys instead. Maybe not
 worth it as users should hit this code path normally.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36433#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/01070197368106ab-18e2ddd8-ce6a-4dc9-a18e-8fc9f5320fdd-000000%40eu-central-1.amazonses.com.

Reply via email to