mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Refs #19527 -- Allowed QuerySet.bulk_create() to set the primary key of its objects.
PostgreSQL support only. Thanks Vladislav Manchev and alesasnouski for working on the patch.
This commit is contained in:
@@ -198,3 +198,22 @@ class BulkCreateTests(TestCase):
|
||||
])
|
||||
bbb = Restaurant.objects.filter(name="betty's beetroot bar")
|
||||
self.assertEqual(bbb.count(), 1)
|
||||
|
||||
@skipUnlessDBFeature('can_return_ids_from_bulk_insert')
|
||||
def test_set_pk_and_insert_single_item(self):
|
||||
countries = []
|
||||
with self.assertNumQueries(1):
|
||||
countries = Country.objects.bulk_create([self.data[0]])
|
||||
self.assertEqual(len(countries), 1)
|
||||
self.assertEqual(Country.objects.get(pk=countries[0].pk), countries[0])
|
||||
|
||||
@skipUnlessDBFeature('can_return_ids_from_bulk_insert')
|
||||
def test_set_pk_and_query_efficiency(self):
|
||||
countries = []
|
||||
with self.assertNumQueries(1):
|
||||
countries = Country.objects.bulk_create(self.data)
|
||||
self.assertEqual(len(countries), 4)
|
||||
self.assertEqual(Country.objects.get(pk=countries[0].pk), countries[0])
|
||||
self.assertEqual(Country.objects.get(pk=countries[1].pk), countries[1])
|
||||
self.assertEqual(Country.objects.get(pk=countries[2].pk), countries[2])
|
||||
self.assertEqual(Country.objects.get(pk=countries[3].pk), countries[3])
|
||||
|
||||
Reference in New Issue
Block a user