* keywords: => bulk insert update upsert
--
Ticket URL: <https://code.djangoproject.com/ticket/34943#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
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>