Skip to content

Instantly share code, notes, and snippets.

@millerdev millerdev/error_on_load.md
Last active Dec 16, 2015

Embed
What would you like to do?

error_on_load column loader strategy for marking column properties that should not be loaded.

It is similar to the deferred loader strategy, but causes an exception to be raised if a column would be loaded by a subsequent (deferred) query. Use error_on_load(k) instead of defer(k) to construct a query option that will cause an exception if an attempt is made to load column k.

This is handy when constructing a query with deferred columns (to optimize load time). It will cause an exception to be raised in your program instead of causing dramatic slowdown due to many deferred-load queries when you accidentally marked a column as deferred that should be loaded eagerly. Fail early.

class NotSelected(Exception): pass
class NotSelectedColumnLoader(sa.orm.interfaces.LoaderStrategy):
"""Provide loading behavior for a error-on-load :class:`.ColumnProperty`."""
def init(self):
self.columns = self.parent_property.columns
def create_row_processor(self, context, path, reduced_path, mapper, row, adapter):
key = self.key
def set_for_local_state(state, dict_, row):
def raise_error(state):
raise NotSelected(key)
state.set_callable(dict_, key, raise_error)
return set_for_local_state, None, None
class NotSelectedOption(sa.orm.interfaces.StrategizedOption):
propagate_to_loaders = True
def get_strategy_class(self):
return NotSelectedColumnLoader
def error_on_load(*key):
"""Return a :class:`.MapperOption` that will raise
:class:`NotSelected` with the given name when its column property
is loaded.
"""
return NotSelectedOption(key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.