MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / raiseload

Method raiseload

lib/sqlalchemy/orm/strategy_options.py:504–540  ·  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

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

Calls 1