Fixed #29126 -- Doc'd the behavior of QuerySet.update_or_create() with manually specified pks.

This commit is contained in:
Tim Graham 2018-02-14 09:57:31 -05:00
parent 7ec0fdf62a
commit e917ea6bec
2 changed files with 17 additions and 0 deletions

View File

@ -1988,6 +1988,10 @@ As described above in :meth:`get_or_create`, this method is prone to a
race-condition which can result in multiple rows being inserted simultaneously race-condition which can result in multiple rows being inserted simultaneously
if uniqueness is not enforced at the database level. if uniqueness is not enforced at the database level.
Like :meth:`get_or_create` and :meth:`create`, if you're using manually
specified primary keys and an object needs to be created but the key already
exists in the database, an :exc:`~django.db.IntegrityError` is raised.
``bulk_create()`` ``bulk_create()``
~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~

View File

@ -444,6 +444,19 @@ class UpdateOrCreateTests(TestCase):
self.assertEqual(obj.last_name, 'NotHarrison') self.assertEqual(obj.last_name, 'NotHarrison')
class UpdateOrCreateTestsWithManualPKs(TestCase):
def test_create_with_duplicate_primary_key(self):
"""
If an existing primary key is specified with different values for other
fields, then IntegrityError is raised and data isn't updated.
"""
ManualPrimaryKeyTest.objects.create(id=1, data='Original')
with self.assertRaises(IntegrityError):
ManualPrimaryKeyTest.objects.update_or_create(id=1, data='Different')
self.assertEqual(ManualPrimaryKeyTest.objects.get(id=1).data, 'Original')
class UpdateOrCreateTransactionTests(TransactionTestCase): class UpdateOrCreateTransactionTests(TransactionTestCase):
available_apps = ['get_or_create'] available_apps = ['get_or_create']