﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
29733	update_or_create - documentation misleading	David Pratten	nobody	"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

"	Cleanup/optimization	closed	Documentation	2.1	Normal	invalid			Unreviewed	0	0	0	0	0	0
