#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:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

 * cc: Vitor Pereira, Chih Sean Hsu (added)
 * type:  Uncategorized => New feature
 * stage:  Unreviewed => Accepted


Old description:

> #34277 added support for `INSERT ... ON CONFLICT(col1, col2) DO UPDATE
> SET ...`  In some cases, however, the unique constraint may be on
> computed columns, which cannot be used with `unique_fields` because they
> are directly quoted.
>
> For instance:
> {{{#!python
> from django.db.models.functions import Upper
> UserTopic.objects.bulk_create(
>   [ut],
>   update_conflicts=True,
>   update_fields=['last_updated','visibility_policy'],
>   unique_fields=['user_profile_id','stream_id',Upper('topic_name')],
> )
> }}}
> ...fails because `UserTopic has no field named 'Upper(F(topic_name))'`.
>
> And:
> {{{#!python
> UserTopic.objects.bulk_create(
>   [ut],
>   update_conflicts=True,
>   update_fields=['last_updated','visibility_policy'],
>   unique_fields=['user_profile_id','stream_id','upper(topic_name)'],
> )
> }}}
> ...fails similarly, with `UserTopic has no field named
> 'upper(topic_name)'`.
>
> It would be a useful feature to be able to handle these cases.

New description:

 #31685 added support for `INSERT ... ON CONFLICT(col1, col2) DO UPDATE SET
 ...`  In some cases, however, the unique constraint may be on computed
 columns, which cannot be used with `unique_fields` because they are
 directly quoted.

 For instance:
 {{{#!python
 from django.db.models.functions import Upper
 UserTopic.objects.bulk_create(
   [ut],
   update_conflicts=True,
   update_fields=['last_updated','visibility_policy'],
   unique_fields=['user_profile_id','stream_id',Upper('topic_name')],
 )
 }}}
 ...fails because `UserTopic has no field named 'Upper(F(topic_name))'`.

 And:
 {{{#!python
 UserTopic.objects.bulk_create(
   [ut],
   update_conflicts=True,
   update_fields=['last_updated','visibility_policy'],
   unique_fields=['user_profile_id','stream_id','upper(topic_name)'],
 )
 }}}
 ...fails similarly, with `UserTopic has no field named
 'upper(topic_name)'`.

 It would be a useful feature to be able to handle these cases.

--

Comment:

 Sounds reasonable.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34943#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 on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018b919db577-9036d1e1-3e97-405d-b790-b0ee8b84d51d-000000%40eu-central-1.amazonses.com.

Reply via email to