Fixed #33799, Refs #31685 -- Added parameters for updating conflicts to QuerySeta.abulk_create().

This commit is contained in:
DhruvaPatil98 2022-06-23 12:32:41 +05:30 committed by Mariusz Felisiak
parent e6f36ea0a9
commit c627226d05
2 changed files with 29 additions and 2 deletions

View File

@ -834,11 +834,22 @@ class QuerySet:
return objs return objs
async def abulk_create(self, objs, batch_size=None, ignore_conflicts=False): async def abulk_create(
self,
objs,
batch_size=None,
ignore_conflicts=False,
update_conflicts=False,
update_fields=None,
unique_fields=None,
):
return await sync_to_async(self.bulk_create)( return await sync_to_async(self.bulk_create)(
objs=objs, objs=objs,
batch_size=batch_size, batch_size=batch_size,
ignore_conflicts=ignore_conflicts, ignore_conflicts=ignore_conflicts,
update_conflicts=update_conflicts,
update_fields=update_fields,
unique_fields=unique_fields,
) )
def bulk_update(self, objs, fields, batch_size=None): def bulk_update(self, objs, fields, batch_size=None):

View File

@ -6,7 +6,7 @@ from asgiref.sync import async_to_sync, sync_to_async
from django.db import NotSupportedError, connection from django.db import NotSupportedError, connection
from django.db.models import Sum from django.db.models import Sum
from django.test import TestCase, skipUnlessDBFeature from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
from .models import SimpleModel from .models import SimpleModel
@ -111,6 +111,22 @@ class AsyncQuerySetTest(TestCase):
qs = await SimpleModel.objects.abulk_create(instances) qs = await SimpleModel.objects.abulk_create(instances)
self.assertEqual(len(qs), 10) self.assertEqual(len(qs), 10)
@skipUnlessDBFeature("has_bulk_insert", "supports_update_conflicts")
@skipIfDBFeature("supports_update_conflicts_with_target")
@async_to_sync
async def test_update_conflicts_unique_field_unsupported(self):
msg = (
"This database backend does not support updating conflicts with specifying "
"unique fields that can trigger the upsert."
)
with self.assertRaisesMessage(NotSupportedError, msg):
await SimpleModel.objects.abulk_create(
[SimpleModel(field=1), SimpleModel(field=2)],
update_conflicts=True,
update_fields=["field"],
unique_fields=["created"],
)
async def test_abulk_update(self): async def test_abulk_update(self):
instances = SimpleModel.objects.all() instances = SimpleModel.objects.all()
async for instance in instances: async for instance in instances: