#34943: Support passing database expressions to bulk_create()'s unique_fields.
-------------------------------------+-------------------------------------
     Reporter:  Alex Vandiver        |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Database layer       |                  Version:  dev
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  bulk insert update   |             Triage Stage:  Accepted
  upsert                             |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

 Maybe the would be an opportunity to deprecate/rename the `unique_fields`
 kwarg name to `unique_expressions` or `unique_together` to denote that not
 only fields are acceptable here.

 Or we could take an hybrid approach like we did with `Index` and
 `Constraint` where either allow `update_fields` or `update_expressions` to
 be specified.

 I get a sense that we should have preferred a `unique_constraint` kwarg
 that points to the name of an entry of `Meta.constraints` or the name of a
 `Field(unique)` in the first place instead of forcing users to specify the
 exact same index definition they have in their model,
 `Meta.unique_together`, or `Meta.constraints` instead.

 For backends that support the `ON CONFLICT ON CONSTRAINT` clause we could
 have simply used it for the constraint name specified otherwise we know
 what the index expression associated with the specified constraint is so
 we can generate the proper SQL.

 In other words, for a model like

 {{{#!python
 class UserTopic(models.Model):
     ...
     class Meta:
         constraints = [
             UniqueConstraint(
                 'user_profile_id','stream_id',Upper('topic_name'),
                 name='unique_profile_stream_topic'
             )
         ]
 }}}

 The call to `bulk_create` could simply have been

 {{{#!python
 UserTopic.objects.bulk_create(
     [ut],
     update_fields=['last_updated','visibility_policy'],
     unique_constraint='unique_profile_stream_topic',
 )

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34943#comment:3>
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/0107018b92943df6-e6bf2f78-a7b2-459d-9fa2-fe68d944ff5f-000000%40eu-central-1.amazonses.com.

Reply via email to