diff options
author | Adrian Moennich <adrian@planetcoding.net> | 2015-08-11 23:08:28 +0200 |
---|---|---|
committer | Adrian Moennich <adrian@planetcoding.net> | 2015-08-12 18:39:43 +0200 |
commit | 6d334da16553c91cbe82de7134042dbe3c511630 (patch) | |
tree | db66cc1463a64ec7cb2d9f986521d3d567e659da /lib/sqlalchemy/orm/strategies.py | |
parent | 5198b1de31029cc985102cd13569086a7056c2f1 (diff) | |
download | sqlalchemy-pr/193.tar.gz |
Add raise/raiseload relationship loading strategypr/193
- available via `lazy='raise'` or by setting the `raiseload` strategy
via `options()`
- behaves almost like `lazy='noload'`, but instead of returning `None`
it raises `InvalidRequestError`
- based on code from Mike Bayer that was posted to the sqlalchemy
mailing list: https://groups.google.com/forum/#!topic/sqlalchemy/X_wA8K97smE
Diffstat (limited to 'lib/sqlalchemy/orm/strategies.py')
-rw-r--r-- | lib/sqlalchemy/orm/strategies.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/sqlalchemy/orm/strategies.py b/lib/sqlalchemy/orm/strategies.py index 67dac1ccc..8fc0a8d68 100644 --- a/lib/sqlalchemy/orm/strategies.py +++ b/lib/sqlalchemy/orm/strategies.py @@ -354,6 +354,33 @@ class NoLoader(AbstractRelationshipLoader): @log.class_logger +@properties.RelationshipProperty.strategy_for(lazy="raise") +class RaiseLoader(NoLoader): + """Provide loading behavior for a :class:`.RelationshipProperty` + with "lazy='raise'". + + """ + + __slots__ = () + + def create_row_processor( + self, context, path, loadopt, mapper, + result, adapter, populators): + + def invoke_raise_load(state, passive): + raise sa_exc.InvalidRequestError( + "'%s' is not available due to lazy='raise'" % self + ) + + set_lazy_callable = InstanceState._instance_level_callable_processor( + mapper.class_manager, + invoke_raise_load, + self.key + ) + populators["new"].append((self.key, set_lazy_callable)) + + +@log.class_logger @properties.RelationshipProperty.strategy_for(lazy=True) @properties.RelationshipProperty.strategy_for(lazy="select") class LazyLoader(AbstractRelationshipLoader, util.MemoizedSlots): |