Skip to content
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
14 changes: 11 additions & 3 deletions psqlextra/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,19 @@ def bulk_insert(
A list of either the dicts of the rows inserted, including the pk or
the models of the rows inserted with defaults for any fields not specified
"""
if rows is None:
return []

def peek(iterable):
try:
first = next(iterable)
except StopIteration:
return None
return list(chain([first], iterable))

def is_empty(r):
return all([False for _ in r])
rows = peek(iter(rows))

if not rows or is_empty(rows):
if not rows:
return []

if not self.conflict_target and not self.conflict_action:
Expand Down
17 changes: 12 additions & 5 deletions tests/test_on_conflict_nothing.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,15 @@ def test_on_conflict_nothing_duplicate_rows():

rows = [dict(amount=1), dict(amount=1)]

(
model.objects.on_conflict(
["amount"], ConflictAction.NOTHING
).bulk_insert(rows)
)
inserted_rows = model.objects.on_conflict(
["amount"], ConflictAction.NOTHING
).bulk_insert(rows)

assert len(inserted_rows) == 1

rows = iter([dict(amount=2), dict(amount=2)])
inserted_rows = model.objects.on_conflict(
["amount"], ConflictAction.NOTHING
).bulk_insert(rows)

assert len(inserted_rows) == 1