From 1bf6b04a0eec6c7b8082826a104c38a72fbce843 Mon Sep 17 00:00:00 2001 From: abvf Date: Thu, 1 Aug 2024 15:56:55 +0330 Subject: [PATCH 1/2] Fix bulk_update --- tortoise/queryset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tortoise/queryset.py b/tortoise/queryset.py index 7248f06ea..ae3ce5079 100644 --- a/tortoise/queryset.py +++ b/tortoise/queryset.py @@ -1800,7 +1800,7 @@ def _make_query(self) -> None: ) executor = self._db.executor_class(model=self.model, db=self._db) pk_attr = self.model._meta.pk_attr - source_pk_attr = self.model._meta.fields_map["id"].source_field or pk_attr + source_pk_attr = self.model._meta.fields_map[pk_attr].source_field or pk_attr pk = Field(source_pk_attr) for objects_item in chunk(self.objects, self.batch_size): query = copy(self.query) From 7a13fc8a30ecf50f008e6d71f8c3304052dc5fb0 Mon Sep 17 00:00:00 2001 From: abvf Date: Sun, 4 Aug 2024 21:14:55 +0000 Subject: [PATCH 2/2] Add test_bulk_update_pk_non_id --- tests/test_update.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/test_update.py b/tests/test_update.py index 2c8dc4e4b..21c142fc5 100644 --- a/tests/test_update.py +++ b/tests/test_update.py @@ -57,6 +57,19 @@ async def test_bulk_update_datetime(self): self.assertEqual((await DatetimeFields.get(pk=objs[0].pk)).datetime, t0) self.assertEqual((await DatetimeFields.get(pk=objs[1].pk)).datetime, t1) + async def test_bulk_update_pk_non_id(self): + tournament = await Tournament.create(name="") + events = [ + await Event.create(name="1", tournament=tournament), + await Event.create(name="2", tournament=tournament), + ] + events[0].name = "3" + events[1].name = "4" + rows_affected = await Event.bulk_update(events, fields=["name"]) + self.assertEqual(rows_affected, 2) + self.assertEqual((await Event.get(pk=events[0].pk)).name, events[0].name) + self.assertEqual((await Event.get(pk=events[1].pk)).name, events[1].name) + async def test_bulk_update_pk_uuid(self): objs = [ await UUIDFields.create(data=uuid.uuid4()),