Skip to content

Commit

Permalink
Fix bulk_update when using source_field for pk (#1633)
Browse files Browse the repository at this point in the history
  • Loading branch information
abondar committed Jun 1, 2024
1 parent 88f0366 commit 035187b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ Changelog
0.21
====

0.21.3
------
Fixed
^^^^^
- Fix `bulk_update` when using source_field for pk (#1633)

0.21.2
------
Added
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "tortoise-orm"
version = "0.21.2"
version = "0.21.3"
description = "Easy async ORM for python, built with relations in mind"
authors = ["Andrey Bondar <andrey@bondar.ru>", "Nickolas Grigoriadis <nagrigoriadis@gmail.com>", "long2ice <long2ice@gmail.com>"]
license = "Apache-2.0"
Expand Down
13 changes: 13 additions & 0 deletions tests/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
JSONFields,
Service,
SmallIntFields,
SourceFieldPk,
Tournament,
UUIDFields,
)
Expand Down Expand Up @@ -68,6 +69,18 @@ async def test_bulk_update_pk_uuid(self):
self.assertEqual((await UUIDFields.get(pk=objs[0].pk)).data, objs[0].data)
self.assertEqual((await UUIDFields.get(pk=objs[1].pk)).data, objs[1].data)

async def test_bulk_renamed_pk_source_field(self):
objs = [
await SourceFieldPk.create(name="Model 1"),
await SourceFieldPk.create(name="Model 2"),
]
objs[0].name = "Model 3"
objs[1].name = "Model 4"
rows_affected = await SourceFieldPk.bulk_update(objs, fields=["name"])
self.assertEqual(rows_affected, 2)
self.assertEqual((await SourceFieldPk.get(pk=objs[0].pk)).name, objs[0].name)
self.assertEqual((await SourceFieldPk.get(pk=objs[1].pk)).name, objs[1].name)

async def test_bulk_update_json_value(self):
objs = [
await JSONFields.create(data={}),
Expand Down
5 changes: 5 additions & 0 deletions tests/testmodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,11 @@ class Meta:
ordering = ["-one"]


class SourceFieldPk(Model):
id = fields.IntField(primary_key=True, source_field="counter")
name = fields.CharField(max_length=255)


class DefaultOrderedInvalid(Model):
one = fields.TextField()
second = fields.IntField()
Expand Down
3 changes: 2 additions & 1 deletion tortoise/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1799,7 +1799,8 @@ def _make_query(self) -> None:
)
executor = self._db.executor_class(model=self.model, db=self._db)
pk_attr = self.model._meta.pk_attr
pk = Field(pk_attr)
source_pk_attr = self.model._meta.fields_map["id"].source_field or pk_attr
pk = Field(source_pk_attr)
for objects_item in chunk(self.objects, self.batch_size):
query = copy(self.query)
for field in self.fields:
Expand Down

0 comments on commit 035187b

Please sign in to comment.