#35575: Add support for constraint validation on GeneratedFields
-------------------------------------+-------------------------------------
     Reporter:  Mark Gensler         |                    Owner:  Mark
                                     |  Gensler
         Type:  New feature          |                   Status:  assigned
    Component:  Database layer       |                  Version:  5.0
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  generatedfield       |             Triage Stage:  Accepted
  uniqueconstraint checkconstraint   |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Comment (by Simon Charette):

 I feel like we should avoid the naive approach of calling
 `refresh_from_db(fields=generated_fields)` as that will mutate the in-
 memory instance and leave it in this form as well as perform an extra
 query per constraint.

 What should be done IMO is for the query performed in `UniqueConstraint`
 to call `replace_expressions({F(gfield.name):
 gfield.expression.replace_expressions(....) for gfield in
 generated_field})` so that will turn things like


 {{{#!python
 class Contributor(models.Model):
     first_name = models.TextField()
     last_name = models.TextField()
     full_name = models.GeneratedField(
         Lower(Concat("first_name", models.Value(" "), "last_name"))
     )

     class Meta:
         constraints = {
             UniqueConstraint(names="unique_full_name", MD5("full_name"))
         }
 }}}

 Then the query to full clean the unique constraint, assuming `first_name`
 and `last_name` are available, would be

 {{{#!sql
 SELECT 1 FROM contributor
 WHERE md5(lower("first_name" || ' ' || "last_name")) = md5(lower('Mark' ||
 ' ' || 'Gensler')
 }}}

 The more we push to the database the less likely we are to run into race
 conditions and serde roudtrip issues.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35575#comment:6>
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 on the web visit 
https://groups.google.com/d/msgid/django-updates/010701907aabed65-08602a9f-372f-451b-b69c-5ed4df2d48e5-000000%40eu-central-1.amazonses.com.

Reply via email to