summaryrefslogtreecommitdiff
path: root/tests/bulk_create
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2022-11-22 14:26:23 +0100
committerGitHub <noreply@github.com>2022-11-22 14:26:23 +0100
commit7d5329852f19c6ae78c6f6f3d3e41835377bf295 (patch)
tree549eeb9d1f63184cf7db1fed67253558076a706e /tests/bulk_create
parent744a1af7f943106e30d538e6ace55c2c66ccd791 (diff)
downloaddjango-7d5329852f19c6ae78c6f6f3d3e41835377bf295.tar.gz
Fixed #34177 -- Fixed QuerySet.bulk_create() crash on "pk" in unique_fields.
Bug in 0f6946495a8ec955b471ca1baaf408ceb53d4796.
Diffstat (limited to 'tests/bulk_create')
-rw-r--r--tests/bulk_create/tests.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/tests/bulk_create/tests.py b/tests/bulk_create/tests.py
index bc2900110d..6c490d1235 100644
--- a/tests/bulk_create/tests.py
+++ b/tests/bulk_create/tests.py
@@ -598,6 +598,39 @@ class BulkCreateTests(TestCase):
@skipUnlessDBFeature(
"supports_update_conflicts", "supports_update_conflicts_with_target"
)
+ def test_update_conflicts_unique_fields_pk(self):
+ TwoFields.objects.bulk_create(
+ [
+ TwoFields(f1=1, f2=1, name="a"),
+ TwoFields(f1=2, f2=2, name="b"),
+ ]
+ )
+ self.assertEqual(TwoFields.objects.count(), 2)
+
+ obj1 = TwoFields.objects.get(f1=1)
+ obj2 = TwoFields.objects.get(f1=2)
+ conflicting_objects = [
+ TwoFields(pk=obj1.pk, f1=3, f2=3, name="c"),
+ TwoFields(pk=obj2.pk, f1=4, f2=4, name="d"),
+ ]
+ TwoFields.objects.bulk_create(
+ conflicting_objects,
+ update_conflicts=True,
+ unique_fields=["pk"],
+ update_fields=["name"],
+ )
+ self.assertEqual(TwoFields.objects.count(), 2)
+ self.assertCountEqual(
+ TwoFields.objects.values("f1", "f2", "name"),
+ [
+ {"f1": 1, "f2": 1, "name": "c"},
+ {"f1": 2, "f2": 2, "name": "d"},
+ ],
+ )
+
+ @skipUnlessDBFeature(
+ "supports_update_conflicts", "supports_update_conflicts_with_target"
+ )
def test_update_conflicts_two_fields_unique_fields_both(self):
with self.assertRaises((OperationalError, ProgrammingError)):
self._test_update_conflicts_two_fields(["f1", "f2"])