Return a "bind" to which this :class:`.Session` is bound. The "bind" is usually an instance of :class:`_engine.Engine`, except in the case where the :class:`.Session` has been explicitly bound directly to a :class:`_engine.Connection`. For a multiply-bound or unboun
(
self,
mapper: Optional[_EntityBindKey[_O]] = None,
*,
clause: Optional[ClauseElement] = None,
bind: Optional[_SessionBind] = None,
_sa_skip_events: Optional[bool] = None,
_sa_skip_for_implicit_returning: bool = False,
**kw: Any,
)
| 2697 | self._add_bind(table, bind) |
| 2698 | |
| 2699 | def get_bind( |
| 2700 | self, |
| 2701 | mapper: Optional[_EntityBindKey[_O]] = None, |
| 2702 | *, |
| 2703 | clause: Optional[ClauseElement] = None, |
| 2704 | bind: Optional[_SessionBind] = None, |
| 2705 | _sa_skip_events: Optional[bool] = None, |
| 2706 | _sa_skip_for_implicit_returning: bool = False, |
| 2707 | **kw: Any, |
| 2708 | ) -> Union[Engine, Connection]: |
| 2709 | """Return a "bind" to which this :class:`.Session` is bound. |
| 2710 | |
| 2711 | The "bind" is usually an instance of :class:`_engine.Engine`, |
| 2712 | except in the case where the :class:`.Session` has been |
| 2713 | explicitly bound directly to a :class:`_engine.Connection`. |
| 2714 | |
| 2715 | For a multiply-bound or unbound :class:`.Session`, the |
| 2716 | ``mapper`` or ``clause`` arguments are used to determine the |
| 2717 | appropriate bind to return. |
| 2718 | |
| 2719 | Note that the "mapper" argument is usually present |
| 2720 | when :meth:`.Session.get_bind` is called via an ORM |
| 2721 | operation such as a :meth:`.Session.query`, each |
| 2722 | individual INSERT/UPDATE/DELETE operation within a |
| 2723 | :meth:`.Session.flush`, call, etc. |
| 2724 | |
| 2725 | The order of resolution is: |
| 2726 | |
| 2727 | 1. if mapper given and :paramref:`.Session.binds` is present, |
| 2728 | locate a bind based first on the mapper in use, then |
| 2729 | on the mapped class in use, then on any base classes that are |
| 2730 | present in the ``__mro__`` of the mapped class, from more specific |
| 2731 | superclasses to more general. |
| 2732 | 2. if clause given and ``Session.binds`` is present, |
| 2733 | locate a bind based on :class:`_schema.Table` objects |
| 2734 | found in the given clause present in ``Session.binds``. |
| 2735 | 3. if ``Session.binds`` is present, return that. |
| 2736 | 4. if clause given, attempt to return a bind |
| 2737 | linked to the :class:`_schema.MetaData` ultimately |
| 2738 | associated with the clause. |
| 2739 | 5. if mapper given, attempt to return a bind |
| 2740 | linked to the :class:`_schema.MetaData` ultimately |
| 2741 | associated with the :class:`_schema.Table` or other |
| 2742 | selectable to which the mapper is mapped. |
| 2743 | 6. No bind can be found, :exc:`~sqlalchemy.exc.UnboundExecutionError` |
| 2744 | is raised. |
| 2745 | |
| 2746 | Note that the :meth:`.Session.get_bind` method can be overridden on |
| 2747 | a user-defined subclass of :class:`.Session` to provide any kind |
| 2748 | of bind resolution scheme. See the example at |
| 2749 | :ref:`session_custom_partitioning`. |
| 2750 | |
| 2751 | :param mapper: |
| 2752 | Optional mapped class or corresponding :class:`_orm.Mapper` instance. |
| 2753 | The bind can be derived from a :class:`_orm.Mapper` first by |
| 2754 | consulting the "binds" map associated with this :class:`.Session`, |
| 2755 | and secondly by consulting the :class:`_schema.MetaData` associated |
| 2756 | with the :class:`_schema.Table` to which the :class:`_orm.Mapper` is |