--- /dev/null
+.. change::
+ :tags: bug, orm
+ :tickets: 13176
+
+ Fixed issue where :meth:`_orm.Session.get` would bypass the identity map
+ and emit unnecessary SQL when ``with_for_update=False`` was passed,
+ rather than treating it equivalently to the default of ``None``.
+ Pull request courtesy of Joshua Swanson.
)
) from err
+ for_update_arg = ForUpdateArg._from_argument(with_for_update)
+
if (
not populate_existing
and not mapper.always_refresh
- and with_for_update is None
+ and for_update_arg is None
):
instance = self._identity_lookup(
mapper,
if populate_existing:
load_options += {"_populate_existing": populate_existing}
statement = sql.select(mapper)
- if with_for_update is not None:
- statement._for_update_arg = ForUpdateArg._from_argument(
- with_for_update
- )
+ if for_update_arg is not None:
+ statement._for_update_arg = for_update_arg
if options:
statement = statement.options(*options)
u2 = s.get(User, 7)
is_not(u, u2)
+ @testing.variation(
+ "with_for_update_arg", ["true", "false", "none", "omitted"]
+ )
+ def test_get_with_for_update_use(self, with_for_update_arg):
+ """test #13176"""
+ users, User = self.tables.users, self.classes.User
+ self.mapper_registry.map_imperatively(User, users)
+
+ s = fixture_session()
+ s.execute(
+ insert(self.tables.users),
+ [{"id": 7, "name": "7"}],
+ )
+ u = s.get(User, 7)
+
+ if with_for_update_arg.true:
+ self.assert_sql_count(
+ testing.db,
+ lambda: s.get(User, 7, with_for_update=True),
+ 1,
+ )
+ elif with_for_update_arg.false:
+ self.assert_sql_count(
+ testing.db,
+ lambda: is_(s.get(User, 7, with_for_update=False), u),
+ 0,
+ )
+ elif with_for_update_arg.none:
+ self.assert_sql_count(
+ testing.db,
+ lambda: is_(s.get(User, 7, with_for_update=None), u),
+ 0,
+ )
+ elif with_for_update_arg.omitted:
+ self.assert_sql_count(
+ testing.db,
+ lambda: is_(s.get(User, 7), u),
+ 0,
+ )
+ else:
+ with_for_update_arg.fail()
+
def test_get_one(self):
users, User = self.tables.users, self.classes.User
self.mapper_registry.map_imperatively(User, users)