Skip to content

Commit 8f6bac6

Browse files
committed
bulk_insert should work with no rows
A fix was previously applied only in `bulk_uspert`.
1 parent d668b76 commit 8f6bac6

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

psqlextra/query.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ def bulk_insert(
139139
the models of the rows inserted with defaults for any fields not specified
140140
"""
141141

142+
def is_empty(r):
143+
return all([False for _ in r])
144+
145+
if not rows or is_empty(rows):
146+
return []
147+
142148
if not self.conflict_target and not self.conflict_action:
143149
# no special action required, use the standard Django bulk_create(..)
144150
return super().bulk_create(
@@ -375,12 +381,6 @@ def bulk_upsert(
375381
the models of the rows upserted
376382
"""
377383

378-
def is_empty(r):
379-
return all([False for _ in r])
380-
381-
if not rows or is_empty(rows):
382-
return []
383-
384384
self.on_conflict(
385385
conflict_target,
386386
ConflictAction.UPDATE,

tests/test_upsert.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from psqlextra.expressions import ExcludedCol
99
from psqlextra.fields import HStoreField
10+
from psqlextra.query import ConflictAction
1011

1112
from .fake_model import get_fake_model
1213

@@ -245,10 +246,18 @@ def test_upsert_bulk_no_rows():
245246
{"name": models.CharField(max_length=255, null=True, unique=True)}
246247
)
247248

249+
model.objects.on_conflict(ConflictAction.UPDATE, ["name"]).bulk_insert(
250+
rows=[]
251+
)
252+
248253
model.objects.bulk_upsert(conflict_target=["name"], rows=[])
249254

250255
model.objects.bulk_upsert(conflict_target=["name"], rows=None)
251256

257+
model.objects.on_conflict(ConflictAction.UPDATE, ["name"]).bulk_insert(
258+
rows=None
259+
)
260+
252261

253262
def test_bulk_upsert_return_models():
254263
"""Tests whether models are returned instead of dictionaries when

0 commit comments

Comments
 (0)