Skip to content

Commit bed0ed8

Browse files
committed
Return zero for one_or_zero when deref'd is None
1 parent f469411 commit bed0ed8

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

postgres/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,12 +394,20 @@ def one_or_zero(self, sql, parameters=None, zero=None, \
394394
>>> db.one_or_zero("SELECT baz FROM foo WHERE bar='buz'")
395395
42
396396
397+
And if the dereferenced value is :py:class:`None`, we return the value
398+
of :py:attr:`zero`:
399+
400+
>>> db.one_or_zero("SELECT sum(baz) FROM foo WHERE bar='nope'", zero=0)
401+
0
402+
397403
"""
398404
out = self._some(sql, parameters, 0, 1, cursor_factory)
399405
if out is None:
400406
out = zero
401407
elif len(out) == 1:
402408
out = out[0]
409+
if out is None:
410+
out = zero
403411
return out
404412

405413

0 commit comments

Comments
 (0)