Skip to content

add support for bulk_update #148

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
change id to model pk name
  • Loading branch information
Micheal Gendy committed Jan 11, 2022
commit 7baf305d535a01141690234defdb3d4d1a68bcb6
4 changes: 2 additions & 2 deletions orm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ async def bulk_update(
]
expr = (
self.table.update()
.where(self.table.c.id == sqlalchemy.bindparam("id"))
.where(self.table.c.id == sqlalchemy.bindparam(self.pkname))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to get primary key column dynamically. The id is hard-coded.

.values(
{
field: sqlalchemy.bindparam(field)
Expand All @@ -498,7 +498,7 @@ async def bulk_update(
}
)
)
pk_list = [{"id": obj.pk} for obj in objs]
pk_list = [{self.pkname: obj.pk} for obj in objs]
joined_list = [{**pk, **value} for pk, value in zip(pk_list, new_objs)]
await self.database.execute_many(str(expr), joined_list)

Expand Down
36 changes: 0 additions & 36 deletions tests/test_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,39 +199,3 @@ async def test_bulk_update():
assert products[1].data == {"foo": 5678}
assert products[0].value == 1234.567
assert products[1].value == 5678.891


async def test_bulk_update_with_relation():
class Album(orm.Model):
registry = models
fields = {
"id": orm.Integer(primary_key=True),
"name": orm.Text(),
}

class Track(orm.Model):
registry = models
fields = {
"id": orm.Integer(primary_key=True),
"name": orm.Text(),
"album": orm.ForeignKey(Album),
}

await models.create_all()

album = await Album.objects.create(name="foo")
album2 = await Album.objects.create(name="bar")

await Track.objects.bulk_create(
[
{"name": "foo", "album": album},
{"name": "bar", "album": album},
]
)
tracks = await Track.objects.all()
for track in tracks:
track.album = album2
await Track.objects.bulk_update(tracks, fields=["album"])
tracks = await Track.objects.all()
assert tracks[0].album.pk == album2.pk
assert tracks[1].album.pk == album2.pk
19 changes: 19 additions & 0 deletions tests/test_foreignkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,22 @@ async def test_nullable_foreign_key():

assert member.email == "dev@encode.io"
assert member.team.pk is None


async def test_bulk_update_with_relation():
album = await Album.objects.create(name="foo")
album2 = await Album.objects.create(name="bar")

await Track.objects.bulk_create(
[
{"name": "foo", "album": album, "position": 1, "title": "foo"},
{"name": "bar", "album": album, "position": 2, "title": "bar"},
]
)
tracks = await Track.objects.all()
for track in tracks:
track.album = album2
await Track.objects.bulk_update(tracks, fields=["album"])
tracks = await Track.objects.all()
assert tracks[0].album.pk == album2.pk
assert tracks[1].album.pk == album2.pk
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy