summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2022-09-19 12:18:47 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2022-09-19 18:27:28 -0400
commit9020e104ebc7f4a0a3a44395c558fb0e119c32ee (patch)
tree84fc1add2f130f374d23822195a25f53227ea1b6 /lib/sqlalchemy
parente7700b4a887c449f33bb5aaa8dfb96f4c76f2a54 (diff)
downloadsqlalchemy-9020e104ebc7f4a0a3a44395c558fb0e119c32ee.tar.gz
add raiseload to load_only()
currently this can't be finessed in another way, at least not easily. The deferred() that it sets up doesn't seem to be cancellable. in any case, this is more consistent API with that of defer(). Change-Id: I30fca1cc371c9102d013cda8e23a522eea1033f8
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r--lib/sqlalchemy/orm/strategy_options.py39
1 files changed, 30 insertions, 9 deletions
diff --git a/lib/sqlalchemy/orm/strategy_options.py b/lib/sqlalchemy/orm/strategy_options.py
index 2aed60b61..2748556fd 100644
--- a/lib/sqlalchemy/orm/strategy_options.py
+++ b/lib/sqlalchemy/orm/strategy_options.py
@@ -165,9 +165,9 @@ class _AbstractLoad(traversals.GenerativeOnTraversal, LoaderOption):
return cloned
def load_only(
- self: Self_AbstractLoad, *attrs: _AttrType
+ self: Self_AbstractLoad, *attrs: _AttrType, raiseload: bool = False
) -> Self_AbstractLoad:
- """Indicate that for a particular entity, only the given list
+ r"""Indicate that for a particular entity, only the given list
of column-based attribute names should be loaded; all others will be
deferred.
@@ -200,14 +200,27 @@ class _AbstractLoad(traversals.GenerativeOnTraversal, LoaderOption):
if the column property is defined with ``deferred=True``
for the :func:`.column_property` function.
+ :param \*attrs: Attributes to be loaded, all others will be deferred.
+
+ :param raiseload: raise :class:`.InvalidRequestError` rather than
+ lazy loading a value when a deferred attribute is accessed. Used
+ to prevent unwanted SQL from being emitted.
+
+ .. versionadded:: 2.0
+
"""
cloned = self._set_column_strategy(
attrs,
{"deferred": False, "instrument": True},
)
+
+ wildcard_strategy = {"deferred": True, "instrument": True}
+ if raiseload:
+ wildcard_strategy["raiseload"] = True
+
cloned = cloned._set_column_strategy(
("*",),
- {"deferred": True, "instrument": True},
+ wildcard_strategy,
)
return cloned
@@ -612,9 +625,9 @@ class _AbstractLoad(traversals.GenerativeOnTraversal, LoaderOption):
:param key: Attribute to be deferred.
- :param raiseload: raise :class:`.InvalidRequestError` if the column
- value is to be loaded from emitting SQL. Used to prevent unwanted
- SQL from being emitted.
+ :param raiseload: raise :class:`.InvalidRequestError` rather than
+ lazy loading a value when the deferred attribute is accessed. Used
+ to prevent unwanted SQL from being emitted.
.. versionadded:: 1.4
@@ -2397,11 +2410,11 @@ def contains_eager(*keys: _AttrType, **kw: Any) -> _AbstractLoad:
@loader_unbound_fn
-def load_only(*attrs: _AttrType) -> _AbstractLoad:
+def load_only(*attrs: _AttrType, raiseload: bool = False) -> _AbstractLoad:
# TODO: attrs against different classes. we likely have to
# add some extra state to Load of some kind
_, lead_element, _ = _parse_attr_argument(attrs[0])
- return Load(lead_element).load_only(*attrs)
+ return Load(lead_element).load_only(*attrs, raiseload=raiseload)
@loader_unbound_fn
@@ -2453,7 +2466,9 @@ def defaultload(*keys: _AttrType) -> _AbstractLoad:
@loader_unbound_fn
-def defer(key: _AttrType, *addl_attrs: _AttrType, **kw: Any) -> _AbstractLoad:
+def defer(
+ key: _AttrType, *addl_attrs: _AttrType, raiseload: bool = False
+) -> _AbstractLoad:
if addl_attrs:
util.warn_deprecated(
"The *addl_attrs on orm.defer is deprecated. Please use "
@@ -2461,6 +2476,12 @@ def defer(key: _AttrType, *addl_attrs: _AttrType, **kw: Any) -> _AbstractLoad:
"indicate a path.",
version="1.3",
)
+
+ if raiseload:
+ kw = {"raiseload": raiseload}
+ else:
+ kw = {}
+
return _generate_from_keys(Load.defer, (key,) + addl_attrs, False, kw)