#20429: Add QuerySet.update_or_create method
-------------------------------------+-------------------------------------
     Reporter:  tunixman             |                    Owner:
         Type:  New feature          |  elektrrrus
    Component:  Database layer       |                   Status:  assigned
  (models, ORM)                      |                  Version:  master
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by loic84):

 The integrity issue is just a side effect of the race-condition, even if
 uniqueness is enforced at the database level, you still need to be aware
 of the potential 500 you will be getting.

 I agree that any `get()` followed by `create()` in user code suffers from
 the same issue, but when you do it manually you have a chance to think
 about the potential problems. There is a layer of opacity when you use a
 queryset method like `update_or_create()` and you may assume that all
 problems have been solved by the framework. Eventually you get to shoot
 yourself in the foot, which results in tickets like #12579.

 `update()` documents that it's free of race-condition,
 `select_for_update()` is obviously free of race-condition on supported
 backends, it wouldn't be too hard to assume that `update_or_create()`
 provides such guarantee as well.

 I agree that the race-condition for these common patterns should be
 documented (if that's not already the case), but convenience methods like
 `get_or_create()` and `update_or_create()` also need to be clear about
 their implementation and potential consequences.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/20429#comment:24>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.f79d23a7c2a482a132e25029d6fca556%40djangoproject.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to