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,
)
| 2791 | self._add_bind(table, bind) |
| 2792 | |
| 2793 | def get_bind( |
| 2794 | self, |
| 2795 | mapper: Optional[_EntityBindKey[_O]] = None, |
| 2796 | *, |
| 2797 | clause: Optional[ClauseElement] = None, |
| 2798 | bind: Optional[_SessionBind] = None, |
| 2799 | _sa_skip_events: Optional[bool] = None, |
| 2800 | _sa_skip_for_implicit_returning: bool = False, |
| 2801 | **kw: Any, |
| 2802 | ) -> Union[Engine, Connection]: |
| 2803 | """Return a "bind" to which this :class:`.Session` is bound. |
| 2804 | |
| 2805 | The "bind" is usually an instance of :class:`_engine.Engine`, |
| 2806 | except in the case where the :class:`.Session` has been |
| 2807 | explicitly bound directly to a :class:`_engine.Connection`. |
| 2808 | |
| 2809 | For a multiply-bound or unbound :class:`.Session`, the |
| 2810 | ``mapper`` or ``clause`` arguments are used to determine the |
| 2811 | appropriate bind to return. |
| 2812 | |
| 2813 | Note that the "mapper" argument is usually present |
| 2814 | when :meth:`.Session.get_bind` is called via an ORM |
| 2815 | operation such as a :meth:`.Session.query`, each |
| 2816 | individual INSERT/UPDATE/DELETE operation within a |
| 2817 | :meth:`.Session.flush`, call, etc. |
| 2818 | |
| 2819 | The order of resolution is: |
| 2820 | |
| 2821 | 1. if mapper given and :paramref:`.Session.binds` is present, |
| 2822 | locate a bind based first on the mapper in use, then |
| 2823 | on the mapped class in use, then on any base classes that are |
| 2824 | present in the ``__mro__`` of the mapped class, from more specific |
| 2825 | superclasses to more general. |
| 2826 | 2. if clause given and ``Session.binds`` is present, |
| 2827 | locate a bind based on :class:`_schema.Table` objects |
| 2828 | found in the given clause present in ``Session.binds``. |
| 2829 | 3. if ``Session.binds`` is present, return that. |
| 2830 | 4. if clause given, attempt to return a bind |
| 2831 | linked to the :class:`_schema.MetaData` ultimately |
| 2832 | associated with the clause. |
| 2833 | 5. if mapper given, attempt to return a bind |
| 2834 | linked to the :class:`_schema.MetaData` ultimately |
| 2835 | associated with the :class:`_schema.Table` or other |
| 2836 | selectable to which the mapper is mapped. |
| 2837 | 6. No bind can be found, :exc:`~sqlalchemy.exc.UnboundExecutionError` |
| 2838 | is raised. |
| 2839 | |
| 2840 | Note that the :meth:`.Session.get_bind` method can be overridden on |
| 2841 | a user-defined subclass of :class:`.Session` to provide any kind |
| 2842 | of bind resolution scheme. See the example at |
| 2843 | :ref:`session_custom_partitioning`. |
| 2844 | |
| 2845 | :param mapper: |
| 2846 | Optional mapped class or corresponding :class:`_orm.Mapper` instance. |
| 2847 | The bind can be derived from a :class:`_orm.Mapper` first by |
| 2848 | consulting the "binds" map associated with this :class:`.Session`, |
| 2849 | and secondly by consulting the :class:`_schema.MetaData` associated |
| 2850 | with the :class:`_schema.Table` to which the :class:`_orm.Mapper` is |