Skip to content

Commit c8d73b7

Browse files
zzzeekGerrit Code Review
authored andcommitted
Merge "call iter() on detached/transient dynamic session" into main
2 parents fb388a1 + d9d98ea commit c8d73b7

3 files changed

Lines changed: 41 additions & 4 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.. change::
2+
:tags: bug, orm, regression
3+
:tickets: 11562
4+
5+
Fixed regression going back to 1.4 where accessing a collection using the
6+
"dynamic" strategy on a transient object and attempting to query would
7+
raise an internal error rather than the expected :class:`.NoResultFound`
8+
that occurred in 1.3.

lib/sqlalchemy/orm/dynamic.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,12 @@ def _iter(self) -> Union[result.ScalarResult[_T], result.Result[_T]]:
161161

162162
return result.IteratorResult(
163163
result.SimpleResultMetaData([self.attr.class_.__name__]),
164-
self.attr._get_collection_history( # type: ignore[arg-type]
165-
attributes.instance_state(self.instance),
166-
PassiveFlag.PASSIVE_NO_INITIALIZE,
167-
).added_items,
164+
iter(
165+
self.attr._get_collection_history(
166+
attributes.instance_state(self.instance),
167+
PassiveFlag.PASSIVE_NO_INITIALIZE,
168+
).added_items
169+
),
168170
_source_supports_scalars=True,
169171
).scalars()
170172
else:

test/orm/test_dynamic.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,33 @@ def my_filter(self, arg):
275275
use_default_dialect=True,
276276
)
277277

278+
@testing.combinations(
279+
("all", []),
280+
("one", exc.NoResultFound),
281+
("one_or_none", None),
282+
argnames="method, expected",
283+
)
284+
@testing.variation("add_to_session", [True, False])
285+
def test_transient_raise(
286+
self, user_address_fixture, method, expected, add_to_session
287+
):
288+
"""test 11562"""
289+
User, Address = user_address_fixture()
290+
291+
u1 = User(name="u1")
292+
if add_to_session:
293+
sess = fixture_session()
294+
sess.add(u1)
295+
296+
meth = getattr(u1.addresses, method)
297+
if expected is exc.NoResultFound:
298+
with expect_raises_message(
299+
exc.NoResultFound, "No row was found when one was required"
300+
):
301+
meth()
302+
else:
303+
eq_(meth(), expected)
304+
278305
def test_detached_raise(self, user_address_fixture):
279306
"""so filtering on a detached dynamic list raises an error..."""
280307

0 commit comments

Comments
 (0)