Skip to content

Commit fae2459

Browse files
committed
Fix one_or_zero to dereference always for 1 col
1 parent 57d5e71 commit fae2459

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

postgres/__init__.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -341,20 +341,14 @@ def all(self, sql, parameters=None):
341341
:type parameters: dict or tuple
342342
:returns: :py:class:`list` of records or single values
343343
344-
>>> for value in db.all("SELECT bar FROM foo"):
345-
... print(value)
346-
...
347-
buz
348-
bit
344+
>>> db.all("SELECT * FROM foo ORDER BY bar")
345+
[Record(bar=u'bit', baz=537), Record(bar=u'buz', baz=42)]
349346
350347
If the query results in a single column, we return a list of values
351348
rather than a list of records of values.
352349
353-
>>> for value in db.all("SELECT bar FROM foo"):
354-
... print(value)
355-
...
356-
buz
357-
bit
350+
>>> db.all("SELECT bar FROM foo ORDER BY bar")
351+
['bit', 'buz']
358352
359353
"""
360354
with self.get_transaction() as txn:
@@ -378,21 +372,25 @@ def one_or_zero(self, sql, parameters=None, zero=None):
378372
Use this for the common case where there should only be one record, but
379373
it may not exist yet.
380374
375+
>>> db.one_or_zero("SELECT * FROM foo WHERE bar='buz'")
376+
Record(bar=u'buz', baz=42)
381377
>>> row = db.one_or_zero("SELECT * FROM foo WHERE bar='blam'")
382378
>>> if row is None:
383379
... print("No blam yet.")
384380
...
385381
No blam yet.
386382
387-
If the query returns one result and that result has one field and the
388-
value of that field is a :py:class:`~postgres.orm.Model` subclass, then
389-
we'll unravel and return just that instance for you.
383+
If the query result has only one column, then we dereference that for
384+
you.
385+
386+
>>> db.one_or_zero("SELECT baz FROM foo WHERE bar='buz'")
387+
42
390388
391389
"""
392390
out = self._some(sql, parameters, 0, 1)
393391
if out is None:
394392
out = zero
395-
elif len(out) == 1 and isinstance(out[0], Model):
393+
elif len(out) == 1:
396394
out = out[0]
397395
return out
398396

postgres/orm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@
9494
That will plug your model into the :py:mod:`psycopg2` composite casting
9595
machinery, and you'll now get instances of your model back from
9696
:py:meth:`~postgres.Postgres.all` and :py:meth:`~postgres.Postgres.one_or_zero`
97-
when you cast your query results to the relevant type. If your query returns
98-
more than one column, you'll need to dereference the column containing the
99-
model just as with any other query:
97+
when you cast to the relevant type in your query. If your query returns more
98+
than one column, you'll need to dereference the column containing the model
99+
just as with any other query:
100100
101101
>>> rec = db.one_or_zero("SELECT foo.*::foo, bar.* "
102102
... "FROM foo JOIN bar ON foo.bar = bar.bar "

0 commit comments

Comments
 (0)