MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / raiseload

Method raiseload

lib/sqlalchemy/orm/strategy_options.py:510–546  ·  view source on GitHub ↗

Indicate that the given attribute should raise an error if accessed. A relationship attribute configured with :func:`_orm.raiseload` will raise an :exc:`~sqlalchemy.exc.InvalidRequestError` upon access. The typical way this is useful is when an application is attempting to

(self, attr: _AttrType, sql_only: bool = False)

Source from the content-addressed store, hash-verified

508 return self._set_relationship_strategy(attr, {"lazy": "noload"})
509
510 def raiseload(self, attr: _AttrType, sql_only: bool = False) -> Self:
511 """Indicate that the given attribute should raise an error if accessed.
512
513 A relationship attribute configured with :func:`_orm.raiseload` will
514 raise an :exc:`~sqlalchemy.exc.InvalidRequestError` upon access. The
515 typical way this is useful is when an application is attempting to
516 ensure that all relationship attributes that are accessed in a
517 particular context would have been already loaded via eager loading.
518 Instead of having to read through SQL logs to ensure lazy loads aren't
519 occurring, this strategy will cause them to raise immediately.
520
521 :func:`_orm.raiseload` applies to :func:`_orm.relationship` attributes
522 only. In order to apply raise-on-SQL behavior to a column-based
523 attribute, use the :paramref:`.orm.defer.raiseload` parameter on the
524 :func:`.defer` loader option.
525
526 :param sql_only: if True, raise only if the lazy load would emit SQL,
527 but not if it is only checking the identity map, or determining that
528 the related value should just be None due to missing keys. When False,
529 the strategy will raise for all varieties of relationship loading.
530
531 This function is part of the :class:`_orm.Load` interface and supports
532 both method-chained and standalone operation.
533
534 .. seealso::
535
536 :ref:`loading_toplevel`
537
538 :ref:`prevent_lazy_with_raiseload`
539
540 :ref:`orm_queryguide_deferred_raiseload`
541
542 """
543
544 return self._set_relationship_strategy(
545 attr, {"lazy": "raise_on_sql" if sql_only else "raise"}
546 )
547
548 def defaultload(self, attr: _AttrType) -> Self:
549 """Indicate an attribute should load using its predefined loader style.

Calls 1