Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bulk_update when using a pk other than id #1689

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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