#29733: update_or_create - documentation misleading
------------------------------------------------+------------------------
               Reporter:  DavidPratten          |          Owner:  nobody
                   Type:  Cleanup/optimization  |         Status:  new
              Component:  Documentation         |        Version:  2.1
               Severity:  Normal                |       Keywords:
           Triage Stage:  Unreviewed            |      Has patch:  0
    Needs documentation:  0                     |    Needs tests:  0
Patch needs improvement:  0                     |  Easy pickings:  0
                  UI/UX:  0                     |
------------------------------------------------+------------------------
 When update_or_create searches the table for a record to update it
 searches all fields marked 'unique' or 'primary_key'.

 In the case where a record has two candidate keys the update_or_create
 function will fail to update either the primary key or the candidate key,
 leading to an Integrity Error.

 https://docs.djangoproject.com/en/2.1/ref/models/querysets/#update-or-
 create

 This behavour is not 'wrong'. But it is surprising and a time waster.

 {{{
 class Coa(models.Model):
     def __str__(self):
         return f'{self.qbac}'
     trantype = models.CharField(max_length=255, db_column=u'TranType',
 blank=True)
     qbac = models.CharField( unique=True, max_length=255,
 db_column=u'QBAc')
     guid = models.CharField(max_length=32, primary_key=True)
     account_type=models.CharField(max_length=20)
 }}}

 Containing the following record:
 {{{
 "ACCNT", "Advantage Saver", "c6f30c15c9dd1e5af9143fd7bed99315", "A"
 }}}

 The following, otherwise sensible, code fails with an IntegrityError

 {{{
 obj, created =
 Coa.objects.update_or_create(guid='c6f30c15c9dd1e5af9143fd7bed99315',
 defaults={'trantype': 'ACCNT', 'qbac': 'A', 'account_type': 'A' },)
 }}}

 because the search is on both guid AND qbac rather than just on guid as
 requested by the single field in the !**kwargs.

 Recommend clarification in the docu that it is not possible to search only
 on one field if there are multiple unique constraints in the model.

 David

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29733>
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/055.7ca3a3a3aaeee11d2de39eb8ce7ad45a%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to