[Django] #34943: Support computed unique fields for INSERT ON CONFLICT ... UPDATE

6 views
Skip to first unread message

Django

unread,
Nov 2, 2023, 3:23:38 PM11/2/23
to django-...@googlegroups.com
#34943: Support computed unique fields for INSERT ON CONFLICT ... UPDATE
-------------------------------------+-------------------------------------
Reporter: Alex | Owner: nobody
Vandiver |
Type: | Status: new
Uncategorized |
Component: Database | Version: dev
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
#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.

--
Ticket URL: <https://code.djangoproject.com/ticket/34943>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Reply all
Reply to author
Forward
0 new messages