Opened 9 years ago

Last modified 8 months ago

#25195 new New feature

update_or_create doesn't understand F() operations

Reported by: Marc Tamlyn Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Ülgen Sarıkavak Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

This may not be possible to resolve, but it would be really nice if you could do something like

Foo.objects.update_or_create(name='bar', defaults={'value': F('value') + 1})

This would only be possible when value has a default, and we would need to resolve the F() object to the default. It may be that we need a different object which resolves to an expression when used in an update but resolves differently when used in a create.

Change History (4)

comment:1 by Tim Graham, 9 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Marc Parizeau, 6 years ago

How about the following?

Foo.objects.update_or_create(name='bar', defaults={'value': Coalesce('value', 0) + 1})

where one could specify an explicit default?

in reply to:  2 comment:3 by Simon Charette, 6 years ago

Replying to Marc Parizeau:

How about the following?

Foo.objects.update_or_create(name='bar', defaults={'value': Coalesce('value', 0) + 1})

where one could specify an explicit default?

We'd need to special case Coalesce in update_or_create for this to work as references to columns are not allowed in INSERTs. In other words, Coalesce('value', 0) + 1 exhibits the same limitations that F('value') + 1 has.

Version 1, edited 6 years ago by Simon Charette (previous) (next) (diff)

comment:4 by Ülgen Sarıkavak, 8 months ago

Cc: Ülgen Sarıkavak added
Note: See TracTickets for help on using tickets.
Back to Top