MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / with_loader_criteria

Function with_loader_criteria

lib/sqlalchemy/orm/_orm_constructors.py:860–1056  ·  view source on GitHub ↗

Add additional WHERE criteria to the load for all occurrences of a particular entity. .. versionadded:: 1.4 The :func:`_orm.with_loader_criteria` option is intended to add limiting criteria to a particular kind of entity in a query, **globally**, meaning it will apply to the en

(
    entity_or_base: _EntityType[Any],
    where_criteria: Union[
        _ColumnExpressionArgument[bool],
        Callable[[Any], _ColumnExpressionArgument[bool]],
    ],
    loader_only: bool = False,
    include_aliases: bool = False,
    propagate_to_loaders: bool = True,
    track_closure_variables: bool = True,
)

Source from the content-addressed store, hash-verified

858
859
860def with_loader_criteria(
861 entity_or_base: _EntityType[Any],
862 where_criteria: Union[
863 _ColumnExpressionArgument[bool],
864 Callable[[Any], _ColumnExpressionArgument[bool]],
865 ],
866 loader_only: bool = False,
867 include_aliases: bool = False,
868 propagate_to_loaders: bool = True,
869 track_closure_variables: bool = True,
870) -> LoaderCriteriaOption:
871 """Add additional WHERE criteria to the load for all occurrences of
872 a particular entity.
873
874 .. versionadded:: 1.4
875
876 The :func:`_orm.with_loader_criteria` option is intended to add
877 limiting criteria to a particular kind of entity in a query,
878 **globally**, meaning it will apply to the entity as it appears
879 in the SELECT query as well as within any subqueries, join
880 conditions, and relationship loads, including both eager and lazy
881 loaders, without the need for it to be specified in any particular
882 part of the query. The rendering logic uses the same system used by
883 single table inheritance to ensure a certain discriminator is applied
884 to a table.
885
886 E.g., using :term:`2.0-style` queries, we can limit the way the
887 ``User.addresses`` collection is loaded, regardless of the kind
888 of loading used::
889
890 from sqlalchemy.orm import with_loader_criteria
891
892 stmt = select(User).options(
893 selectinload(User.addresses),
894 with_loader_criteria(Address, Address.email_address != "foo"),
895 )
896
897 Above, the "selectinload" for ``User.addresses`` will apply the
898 given filtering criteria to the WHERE clause.
899
900 Another example, where the filtering will be applied to the
901 ON clause of the join, in this example using :term:`1.x style`
902 queries::
903
904 q = (
905 session.query(User)
906 .outerjoin(User.addresses)
907 .options(with_loader_criteria(Address, Address.email_address != "foo"))
908 )
909
910 The primary purpose of :func:`_orm.with_loader_criteria` is to use
911 it in the :meth:`_orm.SessionEvents.do_orm_execute` event handler
912 to ensure that all occurrences of a particular entity are filtered
913 in a certain way, such as filtering for access control roles. It
914 also can be used to apply criteria to relationship loads. In the
915 example below, we can apply a certain set of rules to all queries
916 emitted by a particular :class:`_orm.Session`::
917

Calls 1