Skip to content

Commit

Permalink
Fix bulk_update when using a pk other than id (#1689)
Browse files Browse the repository at this point in the history
* Fix bulk_update

* Add test_bulk_update_pk_non_id
  • Loading branch information
abvf authored Aug 5, 2024
1 parent 3b620e4 commit 1353ac1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 13 additions & 0 deletions tests/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
2 changes: 1 addition & 1 deletion tortoise/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1353ac1

Please sign in to comment.