Skip to content

Commit bbfb0ec

Browse files
committed
fix context and update changelog
1 parent c16401e commit bbfb0ec

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

HISTORY.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ GINO 1.1
1111
* Added baked query feature (#478 #659 #667)
1212
* Added ``Query.gino.execution_options`` shortcut (#659)
1313
* Added ``@db.declared_attr(with_table=True)`` (#659)
14+
* [Breaking] empty object instead of ``None`` being returned for objects with values of all selected columns are None (#729)
1415

1516

1617
GINO 1.0

src/gino/loader.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ def __getattr__(self, item):
123123

124124

125125
_none = object()
126+
_none_as_none = object()
126127

127128

128129
def _get_column(model, column_or_name) -> Column:
@@ -213,6 +214,8 @@ def __init__(self, model, *columns, **extras):
213214
self.on_clause = None
214215

215216
def _do_load(self, row, none_as_none):
217+
# none_as_none indicates that in the case of every column of the object is
218+
# None, whether a None or empty instance of the model should be returned.
216219
values = dict((c.name, row[c]) for c in self.columns if c in row)
217220
if none_as_none and all((v is None) for v in values.values()):
218221
return None
@@ -233,27 +236,29 @@ def do_load(self, row, context):
233236
result is distinct.
234237
"""
235238

239+
if context is None:
240+
context = {}
236241
distinct = True
237242
if self._distinct:
238-
if context is None:
239-
context = {}
240243
ctx = context.setdefault(self._distinct, {})
241244
key = tuple(row[col] for col in self._distinct)
242245
rv = ctx.get(key, _none)
243246
if rv is _none:
244-
rv = self._do_load(row, context.get('none_as_none', False))
247+
rv = self._do_load(row, context.get(_none_as_none, False))
245248
ctx[key] = rv
246249
else:
247250
distinct = False
248251
else:
249-
rv = self._do_load(row, context.get('none_as_none', False))
252+
rv = self._do_load(row, context.get(_none_as_none, False))
250253

251254
if rv is None:
252255
return None, None
253256
else:
254257
for key, value in self.extras.items():
255-
context.setdefault('none_as_none', True)
258+
context.setdefault(_none_as_none, True)
256259
value, distinct_ = value.do_load(row, context)
260+
# _none_as_none should not be propagated to parents
261+
context.pop(_none_as_none, 0)
257262
if distinct_ is None:
258263
continue
259264

0 commit comments

Comments
 (0)