r"""Produce an inner join between left and right clauses. :func:`_orm.join` is an extension to the core join interface provided by :func:`_expression.join()`, where the left and right selectable may be not only core selectable objects such as :class:`_schema.Table`, but also mapped
(
left: _FromClauseArgument,
right: _FromClauseArgument,
onclause: Optional[_OnClauseArgument] = None,
isouter: bool = False,
full: bool = False,
)
| 2588 | |
| 2589 | |
| 2590 | def join( |
| 2591 | left: _FromClauseArgument, |
| 2592 | right: _FromClauseArgument, |
| 2593 | onclause: Optional[_OnClauseArgument] = None, |
| 2594 | isouter: bool = False, |
| 2595 | full: bool = False, |
| 2596 | ) -> _ORMJoin: |
| 2597 | r"""Produce an inner join between left and right clauses. |
| 2598 | |
| 2599 | :func:`_orm.join` is an extension to the core join interface |
| 2600 | provided by :func:`_expression.join()`, where the |
| 2601 | left and right selectable may be not only core selectable |
| 2602 | objects such as :class:`_schema.Table`, but also mapped classes or |
| 2603 | :class:`.AliasedClass` instances. The "on" clause can |
| 2604 | be a SQL expression or an ORM mapped attribute |
| 2605 | referencing a configured :func:`_orm.relationship`. |
| 2606 | |
| 2607 | :func:`_orm.join` is not commonly needed in modern usage, |
| 2608 | as its functionality is encapsulated within that of the |
| 2609 | :meth:`_sql.Select.join` and :meth:`_query.Query.join` |
| 2610 | methods. which feature a |
| 2611 | significant amount of automation beyond :func:`_orm.join` |
| 2612 | by itself. Explicit use of :func:`_orm.join` |
| 2613 | with ORM-enabled SELECT statements involves use of the |
| 2614 | :meth:`_sql.Select.select_from` method, as in:: |
| 2615 | |
| 2616 | from sqlalchemy.orm import join |
| 2617 | |
| 2618 | stmt = ( |
| 2619 | select(User) |
| 2620 | .select_from(join(User, Address, User.addresses)) |
| 2621 | .filter(Address.email_address == "foo@bar.com") |
| 2622 | ) |
| 2623 | |
| 2624 | In modern SQLAlchemy the above join can be written more |
| 2625 | succinctly as:: |
| 2626 | |
| 2627 | stmt = ( |
| 2628 | select(User) |
| 2629 | .join(User.addresses) |
| 2630 | .filter(Address.email_address == "foo@bar.com") |
| 2631 | ) |
| 2632 | |
| 2633 | .. warning:: using :func:`_orm.join` directly may not work properly |
| 2634 | with modern ORM options such as :func:`_orm.with_loader_criteria`. |
| 2635 | It is strongly recommended to use the idiomatic join patterns |
| 2636 | provided by methods such as :meth:`.Select.join` and |
| 2637 | :meth:`.Select.join_from` when creating ORM joins. |
| 2638 | |
| 2639 | .. seealso:: |
| 2640 | |
| 2641 | :ref:`orm_queryguide_joins` - in the :ref:`queryguide_toplevel` for |
| 2642 | background on idiomatic ORM join patterns |
| 2643 | |
| 2644 | """ |
| 2645 | return _ORMJoin(left, right, onclause, isouter, full) |
| 2646 | |
| 2647 |