Opened 11 years ago

Closed 3 weeks ago

#25195 closed New feature (needsnewfeatureprocess)

update_or_create doesn't understand F() operations

Reported by: Marc Tamlyn Owned by: Abhay Singh Jadaun
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Ülgen Sarıkavak, Abhay Singh Jadaun, Johannes Maron Triage Stage: Accepted
Has patch: yes 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 (8)

comment:1 by Tim Graham, 11 years ago

Triage Stage: Unreviewed → Accepted

comment:2 by Marc Parizeau, 8 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, 8 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 in create.

Last edited 8 years ago by Simon Charette (previous) (diff)

comment:4 by Ülgen Sarıkavak, 3 years ago

Cc: Ülgen Sarıkavak added

comment:5 by Yujin Kim, 7 months ago

Owner: changed from nobody to Yujin Kim
Status: new → assigned

comment:6 by Abhay Singh Jadaun, 3 weeks ago

Cc: Abhay Singh Jadaun added
Owner: changed from Yujin Kim to Abhay Singh Jadaun

comment:7 by Abhay Singh Jadaun, 3 weeks ago

Has patch: set

comment:8 by Johannes Maron, 3 weeks ago

Cc: Johannes Maron added
Resolution: → needsnewfeatureprocess
Status: assigned → closed

Howdy,

I took my good time to look into this issue. I currently have multiple open questions on how this is supposed to work. Excepting this, using the fields' default or db_default value is very opaque IMHO. Since the issue is really old, I believe it warrants a bit more conceptual work in a new-feature issue and possibly a small DEP.

Sorry if this wasn't the outcome you're hoping for, but all good things take time. :)

Cheers
-Joe

Note: See TracTickets for help on using tickets.
Back to Top