scoping.py 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162
  1. # orm/scoping.py
  2. # Copyright (C) 2005-2025 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. from __future__ import annotations
  8. from typing import Any
  9. from typing import Callable
  10. from typing import Dict
  11. from typing import Generic
  12. from typing import Iterable
  13. from typing import Iterator
  14. from typing import Optional
  15. from typing import overload
  16. from typing import Sequence
  17. from typing import Tuple
  18. from typing import Type
  19. from typing import TYPE_CHECKING
  20. from typing import TypeVar
  21. from typing import Union
  22. from .session import _S
  23. from .session import Session
  24. from .. import exc as sa_exc
  25. from .. import util
  26. from ..util import create_proxy_methods
  27. from ..util import ScopedRegistry
  28. from ..util import ThreadLocalRegistry
  29. from ..util import warn
  30. from ..util import warn_deprecated
  31. from ..util.typing import Protocol
  32. if TYPE_CHECKING:
  33. from ._typing import _EntityType
  34. from ._typing import _IdentityKeyType
  35. from ._typing import OrmExecuteOptionsParameter
  36. from .identity import IdentityMap
  37. from .interfaces import ORMOption
  38. from .mapper import Mapper
  39. from .query import Query
  40. from .query import RowReturningQuery
  41. from .session import _BindArguments
  42. from .session import _EntityBindKey
  43. from .session import _PKIdentityArgument
  44. from .session import _SessionBind
  45. from .session import sessionmaker
  46. from .session import SessionTransaction
  47. from ..engine import Connection
  48. from ..engine import CursorResult
  49. from ..engine import Engine
  50. from ..engine import Result
  51. from ..engine import Row
  52. from ..engine import RowMapping
  53. from ..engine.interfaces import _CoreAnyExecuteParams
  54. from ..engine.interfaces import _CoreSingleExecuteParams
  55. from ..engine.interfaces import CoreExecuteOptionsParameter
  56. from ..engine.result import ScalarResult
  57. from ..sql._typing import _ColumnsClauseArgument
  58. from ..sql._typing import _T0
  59. from ..sql._typing import _T1
  60. from ..sql._typing import _T2
  61. from ..sql._typing import _T3
  62. from ..sql._typing import _T4
  63. from ..sql._typing import _T5
  64. from ..sql._typing import _T6
  65. from ..sql._typing import _T7
  66. from ..sql._typing import _TypedColumnClauseArgument as _TCCA
  67. from ..sql.base import Executable
  68. from ..sql.dml import UpdateBase
  69. from ..sql.elements import ClauseElement
  70. from ..sql.roles import TypedColumnsClauseRole
  71. from ..sql.selectable import ForUpdateParameter
  72. from ..sql.selectable import TypedReturnsRows
  73. _T = TypeVar("_T", bound=Any)
  74. class QueryPropertyDescriptor(Protocol):
  75. """Describes the type applied to a class-level
  76. :meth:`_orm.scoped_session.query_property` attribute.
  77. .. versionadded:: 2.0.5
  78. """
  79. def __get__(self, instance: Any, owner: Type[_T]) -> Query[_T]: ...
  80. _O = TypeVar("_O", bound=object)
  81. __all__ = ["scoped_session"]
  82. @create_proxy_methods(
  83. Session,
  84. ":class:`_orm.Session`",
  85. ":class:`_orm.scoping.scoped_session`",
  86. classmethods=["close_all", "object_session", "identity_key"],
  87. methods=[
  88. "__contains__",
  89. "__iter__",
  90. "add",
  91. "add_all",
  92. "begin",
  93. "begin_nested",
  94. "close",
  95. "reset",
  96. "commit",
  97. "connection",
  98. "delete",
  99. "execute",
  100. "expire",
  101. "expire_all",
  102. "expunge",
  103. "expunge_all",
  104. "flush",
  105. "get",
  106. "get_one",
  107. "get_bind",
  108. "is_modified",
  109. "bulk_save_objects",
  110. "bulk_insert_mappings",
  111. "bulk_update_mappings",
  112. "merge",
  113. "query",
  114. "refresh",
  115. "rollback",
  116. "scalar",
  117. "scalars",
  118. ],
  119. attributes=[
  120. "bind",
  121. "dirty",
  122. "deleted",
  123. "new",
  124. "identity_map",
  125. "is_active",
  126. "autoflush",
  127. "no_autoflush",
  128. "info",
  129. ],
  130. )
  131. class scoped_session(Generic[_S]):
  132. """Provides scoped management of :class:`.Session` objects.
  133. See :ref:`unitofwork_contextual` for a tutorial.
  134. .. note::
  135. When using :ref:`asyncio_toplevel`, the async-compatible
  136. :class:`_asyncio.async_scoped_session` class should be
  137. used in place of :class:`.scoped_session`.
  138. """
  139. _support_async: bool = False
  140. session_factory: sessionmaker[_S]
  141. """The `session_factory` provided to `__init__` is stored in this
  142. attribute and may be accessed at a later time. This can be useful when
  143. a new non-scoped :class:`.Session` is needed."""
  144. registry: ScopedRegistry[_S]
  145. def __init__(
  146. self,
  147. session_factory: sessionmaker[_S],
  148. scopefunc: Optional[Callable[[], Any]] = None,
  149. ):
  150. """Construct a new :class:`.scoped_session`.
  151. :param session_factory: a factory to create new :class:`.Session`
  152. instances. This is usually, but not necessarily, an instance
  153. of :class:`.sessionmaker`.
  154. :param scopefunc: optional function which defines
  155. the current scope. If not passed, the :class:`.scoped_session`
  156. object assumes "thread-local" scope, and will use
  157. a Python ``threading.local()`` in order to maintain the current
  158. :class:`.Session`. If passed, the function should return
  159. a hashable token; this token will be used as the key in a
  160. dictionary in order to store and retrieve the current
  161. :class:`.Session`.
  162. """
  163. self.session_factory = session_factory
  164. if scopefunc:
  165. self.registry = ScopedRegistry(session_factory, scopefunc)
  166. else:
  167. self.registry = ThreadLocalRegistry(session_factory)
  168. @property
  169. def _proxied(self) -> _S:
  170. return self.registry()
  171. def __call__(self, **kw: Any) -> _S:
  172. r"""Return the current :class:`.Session`, creating it
  173. using the :attr:`.scoped_session.session_factory` if not present.
  174. :param \**kw: Keyword arguments will be passed to the
  175. :attr:`.scoped_session.session_factory` callable, if an existing
  176. :class:`.Session` is not present. If the :class:`.Session` is present
  177. and keyword arguments have been passed,
  178. :exc:`~sqlalchemy.exc.InvalidRequestError` is raised.
  179. """
  180. if kw:
  181. if self.registry.has():
  182. raise sa_exc.InvalidRequestError(
  183. "Scoped session is already present; "
  184. "no new arguments may be specified."
  185. )
  186. else:
  187. sess = self.session_factory(**kw)
  188. self.registry.set(sess)
  189. else:
  190. sess = self.registry()
  191. if not self._support_async and sess._is_asyncio:
  192. warn_deprecated(
  193. "Using `scoped_session` with asyncio is deprecated and "
  194. "will raise an error in a future version. "
  195. "Please use `async_scoped_session` instead.",
  196. "1.4.23",
  197. )
  198. return sess
  199. def configure(self, **kwargs: Any) -> None:
  200. """reconfigure the :class:`.sessionmaker` used by this
  201. :class:`.scoped_session`.
  202. See :meth:`.sessionmaker.configure`.
  203. """
  204. if self.registry.has():
  205. warn(
  206. "At least one scoped session is already present. "
  207. " configure() can not affect sessions that have "
  208. "already been created."
  209. )
  210. self.session_factory.configure(**kwargs)
  211. def remove(self) -> None:
  212. """Dispose of the current :class:`.Session`, if present.
  213. This will first call :meth:`.Session.close` method
  214. on the current :class:`.Session`, which releases any existing
  215. transactional/connection resources still being held; transactions
  216. specifically are rolled back. The :class:`.Session` is then
  217. discarded. Upon next usage within the same scope,
  218. the :class:`.scoped_session` will produce a new
  219. :class:`.Session` object.
  220. """
  221. if self.registry.has():
  222. self.registry().close()
  223. self.registry.clear()
  224. def query_property(
  225. self, query_cls: Optional[Type[Query[_T]]] = None
  226. ) -> QueryPropertyDescriptor:
  227. """return a class property which produces a legacy
  228. :class:`_query.Query` object against the class and the current
  229. :class:`.Session` when called.
  230. .. legacy:: The :meth:`_orm.scoped_session.query_property` accessor
  231. is specific to the legacy :class:`.Query` object and is not
  232. considered to be part of :term:`2.0-style` ORM use.
  233. e.g.::
  234. from sqlalchemy.orm import QueryPropertyDescriptor
  235. from sqlalchemy.orm import scoped_session
  236. from sqlalchemy.orm import sessionmaker
  237. Session = scoped_session(sessionmaker())
  238. class MyClass:
  239. query: QueryPropertyDescriptor = Session.query_property()
  240. # after mappers are defined
  241. result = MyClass.query.filter(MyClass.name == "foo").all()
  242. Produces instances of the session's configured query class by
  243. default. To override and use a custom implementation, provide
  244. a ``query_cls`` callable. The callable will be invoked with
  245. the class's mapper as a positional argument and a session
  246. keyword argument.
  247. There is no limit to the number of query properties placed on
  248. a class.
  249. """
  250. class query:
  251. def __get__(s, instance: Any, owner: Type[_O]) -> Query[_O]:
  252. if query_cls:
  253. # custom query class
  254. return query_cls(owner, session=self.registry()) # type: ignore # noqa: E501
  255. else:
  256. # session's configured query class
  257. return self.registry().query(owner)
  258. return query()
  259. # START PROXY METHODS scoped_session
  260. # code within this block is **programmatically,
  261. # statically generated** by tools/generate_proxy_methods.py
  262. def __contains__(self, instance: object) -> bool:
  263. r"""Return True if the instance is associated with this session.
  264. .. container:: class_bases
  265. Proxied for the :class:`_orm.Session` class on
  266. behalf of the :class:`_orm.scoping.scoped_session` class.
  267. The instance may be pending or persistent within the Session for a
  268. result of True.
  269. """ # noqa: E501
  270. return self._proxied.__contains__(instance)
  271. def __iter__(self) -> Iterator[object]:
  272. r"""Iterate over all pending or persistent instances within this
  273. Session.
  274. .. container:: class_bases
  275. Proxied for the :class:`_orm.Session` class on
  276. behalf of the :class:`_orm.scoping.scoped_session` class.
  277. """ # noqa: E501
  278. return self._proxied.__iter__()
  279. def add(self, instance: object, _warn: bool = True) -> None:
  280. r"""Place an object into this :class:`_orm.Session`.
  281. .. container:: class_bases
  282. Proxied for the :class:`_orm.Session` class on
  283. behalf of the :class:`_orm.scoping.scoped_session` class.
  284. Objects that are in the :term:`transient` state when passed to the
  285. :meth:`_orm.Session.add` method will move to the
  286. :term:`pending` state, until the next flush, at which point they
  287. will move to the :term:`persistent` state.
  288. Objects that are in the :term:`detached` state when passed to the
  289. :meth:`_orm.Session.add` method will move to the :term:`persistent`
  290. state directly.
  291. If the transaction used by the :class:`_orm.Session` is rolled back,
  292. objects which were transient when they were passed to
  293. :meth:`_orm.Session.add` will be moved back to the
  294. :term:`transient` state, and will no longer be present within this
  295. :class:`_orm.Session`.
  296. .. seealso::
  297. :meth:`_orm.Session.add_all`
  298. :ref:`session_adding` - at :ref:`session_basics`
  299. """ # noqa: E501
  300. return self._proxied.add(instance, _warn=_warn)
  301. def add_all(self, instances: Iterable[object]) -> None:
  302. r"""Add the given collection of instances to this :class:`_orm.Session`.
  303. .. container:: class_bases
  304. Proxied for the :class:`_orm.Session` class on
  305. behalf of the :class:`_orm.scoping.scoped_session` class.
  306. See the documentation for :meth:`_orm.Session.add` for a general
  307. behavioral description.
  308. .. seealso::
  309. :meth:`_orm.Session.add`
  310. :ref:`session_adding` - at :ref:`session_basics`
  311. """ # noqa: E501
  312. return self._proxied.add_all(instances)
  313. def begin(self, nested: bool = False) -> SessionTransaction:
  314. r"""Begin a transaction, or nested transaction,
  315. on this :class:`.Session`, if one is not already begun.
  316. .. container:: class_bases
  317. Proxied for the :class:`_orm.Session` class on
  318. behalf of the :class:`_orm.scoping.scoped_session` class.
  319. The :class:`_orm.Session` object features **autobegin** behavior,
  320. so that normally it is not necessary to call the
  321. :meth:`_orm.Session.begin`
  322. method explicitly. However, it may be used in order to control
  323. the scope of when the transactional state is begun.
  324. When used to begin the outermost transaction, an error is raised
  325. if this :class:`.Session` is already inside of a transaction.
  326. :param nested: if True, begins a SAVEPOINT transaction and is
  327. equivalent to calling :meth:`~.Session.begin_nested`. For
  328. documentation on SAVEPOINT transactions, please see
  329. :ref:`session_begin_nested`.
  330. :return: the :class:`.SessionTransaction` object. Note that
  331. :class:`.SessionTransaction`
  332. acts as a Python context manager, allowing :meth:`.Session.begin`
  333. to be used in a "with" block. See :ref:`session_explicit_begin` for
  334. an example.
  335. .. seealso::
  336. :ref:`session_autobegin`
  337. :ref:`unitofwork_transaction`
  338. :meth:`.Session.begin_nested`
  339. """ # noqa: E501
  340. return self._proxied.begin(nested=nested)
  341. def begin_nested(self) -> SessionTransaction:
  342. r"""Begin a "nested" transaction on this Session, e.g. SAVEPOINT.
  343. .. container:: class_bases
  344. Proxied for the :class:`_orm.Session` class on
  345. behalf of the :class:`_orm.scoping.scoped_session` class.
  346. The target database(s) and associated drivers must support SQL
  347. SAVEPOINT for this method to function correctly.
  348. For documentation on SAVEPOINT
  349. transactions, please see :ref:`session_begin_nested`.
  350. :return: the :class:`.SessionTransaction` object. Note that
  351. :class:`.SessionTransaction` acts as a context manager, allowing
  352. :meth:`.Session.begin_nested` to be used in a "with" block.
  353. See :ref:`session_begin_nested` for a usage example.
  354. .. seealso::
  355. :ref:`session_begin_nested`
  356. :ref:`pysqlite_serializable` - special workarounds required
  357. with the SQLite driver in order for SAVEPOINT to work
  358. correctly. For asyncio use cases, see the section
  359. :ref:`aiosqlite_serializable`.
  360. """ # noqa: E501
  361. return self._proxied.begin_nested()
  362. def close(self) -> None:
  363. r"""Close out the transactional resources and ORM objects used by this
  364. :class:`_orm.Session`.
  365. .. container:: class_bases
  366. Proxied for the :class:`_orm.Session` class on
  367. behalf of the :class:`_orm.scoping.scoped_session` class.
  368. This expunges all ORM objects associated with this
  369. :class:`_orm.Session`, ends any transaction in progress and
  370. :term:`releases` any :class:`_engine.Connection` objects which this
  371. :class:`_orm.Session` itself has checked out from associated
  372. :class:`_engine.Engine` objects. The operation then leaves the
  373. :class:`_orm.Session` in a state which it may be used again.
  374. .. tip::
  375. In the default running mode the :meth:`_orm.Session.close`
  376. method **does not prevent the Session from being used again**.
  377. The :class:`_orm.Session` itself does not actually have a
  378. distinct "closed" state; it merely means
  379. the :class:`_orm.Session` will release all database connections
  380. and ORM objects.
  381. Setting the parameter :paramref:`_orm.Session.close_resets_only`
  382. to ``False`` will instead make the ``close`` final, meaning that
  383. any further action on the session will be forbidden.
  384. .. versionchanged:: 1.4 The :meth:`.Session.close` method does not
  385. immediately create a new :class:`.SessionTransaction` object;
  386. instead, the new :class:`.SessionTransaction` is created only if
  387. the :class:`.Session` is used again for a database operation.
  388. .. seealso::
  389. :ref:`session_closing` - detail on the semantics of
  390. :meth:`_orm.Session.close` and :meth:`_orm.Session.reset`.
  391. :meth:`_orm.Session.reset` - a similar method that behaves like
  392. ``close()`` with the parameter
  393. :paramref:`_orm.Session.close_resets_only` set to ``True``.
  394. """ # noqa: E501
  395. return self._proxied.close()
  396. def reset(self) -> None:
  397. r"""Close out the transactional resources and ORM objects used by this
  398. :class:`_orm.Session`, resetting the session to its initial state.
  399. .. container:: class_bases
  400. Proxied for the :class:`_orm.Session` class on
  401. behalf of the :class:`_orm.scoping.scoped_session` class.
  402. This method provides for same "reset-only" behavior that the
  403. :meth:`_orm.Session.close` method has provided historically, where the
  404. state of the :class:`_orm.Session` is reset as though the object were
  405. brand new, and ready to be used again.
  406. This method may then be useful for :class:`_orm.Session` objects
  407. which set :paramref:`_orm.Session.close_resets_only` to ``False``,
  408. so that "reset only" behavior is still available.
  409. .. versionadded:: 2.0.22
  410. .. seealso::
  411. :ref:`session_closing` - detail on the semantics of
  412. :meth:`_orm.Session.close` and :meth:`_orm.Session.reset`.
  413. :meth:`_orm.Session.close` - a similar method will additionally
  414. prevent re-use of the Session when the parameter
  415. :paramref:`_orm.Session.close_resets_only` is set to ``False``.
  416. """ # noqa: E501
  417. return self._proxied.reset()
  418. def commit(self) -> None:
  419. r"""Flush pending changes and commit the current transaction.
  420. .. container:: class_bases
  421. Proxied for the :class:`_orm.Session` class on
  422. behalf of the :class:`_orm.scoping.scoped_session` class.
  423. When the COMMIT operation is complete, all objects are fully
  424. :term:`expired`, erasing their internal contents, which will be
  425. automatically re-loaded when the objects are next accessed. In the
  426. interim, these objects are in an expired state and will not function if
  427. they are :term:`detached` from the :class:`.Session`. Additionally,
  428. this re-load operation is not supported when using asyncio-oriented
  429. APIs. The :paramref:`.Session.expire_on_commit` parameter may be used
  430. to disable this behavior.
  431. When there is no transaction in place for the :class:`.Session`,
  432. indicating that no operations were invoked on this :class:`.Session`
  433. since the previous call to :meth:`.Session.commit`, the method will
  434. begin and commit an internal-only "logical" transaction, that does not
  435. normally affect the database unless pending flush changes were
  436. detected, but will still invoke event handlers and object expiration
  437. rules.
  438. The outermost database transaction is committed unconditionally,
  439. automatically releasing any SAVEPOINTs in effect.
  440. .. seealso::
  441. :ref:`session_committing`
  442. :ref:`unitofwork_transaction`
  443. :ref:`asyncio_orm_avoid_lazyloads`
  444. """ # noqa: E501
  445. return self._proxied.commit()
  446. def connection(
  447. self,
  448. bind_arguments: Optional[_BindArguments] = None,
  449. execution_options: Optional[CoreExecuteOptionsParameter] = None,
  450. ) -> Connection:
  451. r"""Return a :class:`_engine.Connection` object corresponding to this
  452. :class:`.Session` object's transactional state.
  453. .. container:: class_bases
  454. Proxied for the :class:`_orm.Session` class on
  455. behalf of the :class:`_orm.scoping.scoped_session` class.
  456. Either the :class:`_engine.Connection` corresponding to the current
  457. transaction is returned, or if no transaction is in progress, a new
  458. one is begun and the :class:`_engine.Connection`
  459. returned (note that no
  460. transactional state is established with the DBAPI until the first
  461. SQL statement is emitted).
  462. Ambiguity in multi-bind or unbound :class:`.Session` objects can be
  463. resolved through any of the optional keyword arguments. This
  464. ultimately makes usage of the :meth:`.get_bind` method for resolution.
  465. :param bind_arguments: dictionary of bind arguments. May include
  466. "mapper", "bind", "clause", other custom arguments that are passed
  467. to :meth:`.Session.get_bind`.
  468. :param execution_options: a dictionary of execution options that will
  469. be passed to :meth:`_engine.Connection.execution_options`, **when the
  470. connection is first procured only**. If the connection is already
  471. present within the :class:`.Session`, a warning is emitted and
  472. the arguments are ignored.
  473. .. seealso::
  474. :ref:`session_transaction_isolation`
  475. """ # noqa: E501
  476. return self._proxied.connection(
  477. bind_arguments=bind_arguments, execution_options=execution_options
  478. )
  479. def delete(self, instance: object) -> None:
  480. r"""Mark an instance as deleted.
  481. .. container:: class_bases
  482. Proxied for the :class:`_orm.Session` class on
  483. behalf of the :class:`_orm.scoping.scoped_session` class.
  484. The object is assumed to be either :term:`persistent` or
  485. :term:`detached` when passed; after the method is called, the
  486. object will remain in the :term:`persistent` state until the next
  487. flush proceeds. During this time, the object will also be a member
  488. of the :attr:`_orm.Session.deleted` collection.
  489. When the next flush proceeds, the object will move to the
  490. :term:`deleted` state, indicating a ``DELETE`` statement was emitted
  491. for its row within the current transaction. When the transaction
  492. is successfully committed,
  493. the deleted object is moved to the :term:`detached` state and is
  494. no longer present within this :class:`_orm.Session`.
  495. .. seealso::
  496. :ref:`session_deleting` - at :ref:`session_basics`
  497. """ # noqa: E501
  498. return self._proxied.delete(instance)
  499. @overload
  500. def execute(
  501. self,
  502. statement: TypedReturnsRows[_T],
  503. params: Optional[_CoreAnyExecuteParams] = None,
  504. *,
  505. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  506. bind_arguments: Optional[_BindArguments] = None,
  507. _parent_execute_state: Optional[Any] = None,
  508. _add_event: Optional[Any] = None,
  509. ) -> Result[_T]: ...
  510. @overload
  511. def execute(
  512. self,
  513. statement: UpdateBase,
  514. params: Optional[_CoreAnyExecuteParams] = None,
  515. *,
  516. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  517. bind_arguments: Optional[_BindArguments] = None,
  518. _parent_execute_state: Optional[Any] = None,
  519. _add_event: Optional[Any] = None,
  520. ) -> CursorResult[Any]: ...
  521. @overload
  522. def execute(
  523. self,
  524. statement: Executable,
  525. params: Optional[_CoreAnyExecuteParams] = None,
  526. *,
  527. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  528. bind_arguments: Optional[_BindArguments] = None,
  529. _parent_execute_state: Optional[Any] = None,
  530. _add_event: Optional[Any] = None,
  531. ) -> Result[Any]: ...
  532. def execute(
  533. self,
  534. statement: Executable,
  535. params: Optional[_CoreAnyExecuteParams] = None,
  536. *,
  537. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  538. bind_arguments: Optional[_BindArguments] = None,
  539. _parent_execute_state: Optional[Any] = None,
  540. _add_event: Optional[Any] = None,
  541. ) -> Result[Any]:
  542. r"""Execute a SQL expression construct.
  543. .. container:: class_bases
  544. Proxied for the :class:`_orm.Session` class on
  545. behalf of the :class:`_orm.scoping.scoped_session` class.
  546. Returns a :class:`_engine.Result` object representing
  547. results of the statement execution.
  548. E.g.::
  549. from sqlalchemy import select
  550. result = session.execute(select(User).where(User.id == 5))
  551. The API contract of :meth:`_orm.Session.execute` is similar to that
  552. of :meth:`_engine.Connection.execute`, the :term:`2.0 style` version
  553. of :class:`_engine.Connection`.
  554. .. versionchanged:: 1.4 the :meth:`_orm.Session.execute` method is
  555. now the primary point of ORM statement execution when using
  556. :term:`2.0 style` ORM usage.
  557. :param statement:
  558. An executable statement (i.e. an :class:`.Executable` expression
  559. such as :func:`_expression.select`).
  560. :param params:
  561. Optional dictionary, or list of dictionaries, containing
  562. bound parameter values. If a single dictionary, single-row
  563. execution occurs; if a list of dictionaries, an
  564. "executemany" will be invoked. The keys in each dictionary
  565. must correspond to parameter names present in the statement.
  566. :param execution_options: optional dictionary of execution options,
  567. which will be associated with the statement execution. This
  568. dictionary can provide a subset of the options that are accepted
  569. by :meth:`_engine.Connection.execution_options`, and may also
  570. provide additional options understood only in an ORM context.
  571. .. seealso::
  572. :ref:`orm_queryguide_execution_options` - ORM-specific execution
  573. options
  574. :param bind_arguments: dictionary of additional arguments to determine
  575. the bind. May include "mapper", "bind", or other custom arguments.
  576. Contents of this dictionary are passed to the
  577. :meth:`.Session.get_bind` method.
  578. :return: a :class:`_engine.Result` object.
  579. """ # noqa: E501
  580. return self._proxied.execute(
  581. statement,
  582. params=params,
  583. execution_options=execution_options,
  584. bind_arguments=bind_arguments,
  585. _parent_execute_state=_parent_execute_state,
  586. _add_event=_add_event,
  587. )
  588. def expire(
  589. self, instance: object, attribute_names: Optional[Iterable[str]] = None
  590. ) -> None:
  591. r"""Expire the attributes on an instance.
  592. .. container:: class_bases
  593. Proxied for the :class:`_orm.Session` class on
  594. behalf of the :class:`_orm.scoping.scoped_session` class.
  595. Marks the attributes of an instance as out of date. When an expired
  596. attribute is next accessed, a query will be issued to the
  597. :class:`.Session` object's current transactional context in order to
  598. load all expired attributes for the given instance. Note that
  599. a highly isolated transaction will return the same values as were
  600. previously read in that same transaction, regardless of changes
  601. in database state outside of that transaction.
  602. To expire all objects in the :class:`.Session` simultaneously,
  603. use :meth:`Session.expire_all`.
  604. The :class:`.Session` object's default behavior is to
  605. expire all state whenever the :meth:`Session.rollback`
  606. or :meth:`Session.commit` methods are called, so that new
  607. state can be loaded for the new transaction. For this reason,
  608. calling :meth:`Session.expire` only makes sense for the specific
  609. case that a non-ORM SQL statement was emitted in the current
  610. transaction.
  611. :param instance: The instance to be refreshed.
  612. :param attribute_names: optional list of string attribute names
  613. indicating a subset of attributes to be expired.
  614. .. seealso::
  615. :ref:`session_expire` - introductory material
  616. :meth:`.Session.expire`
  617. :meth:`.Session.refresh`
  618. :meth:`_orm.Query.populate_existing`
  619. """ # noqa: E501
  620. return self._proxied.expire(instance, attribute_names=attribute_names)
  621. def expire_all(self) -> None:
  622. r"""Expires all persistent instances within this Session.
  623. .. container:: class_bases
  624. Proxied for the :class:`_orm.Session` class on
  625. behalf of the :class:`_orm.scoping.scoped_session` class.
  626. When any attributes on a persistent instance is next accessed,
  627. a query will be issued using the
  628. :class:`.Session` object's current transactional context in order to
  629. load all expired attributes for the given instance. Note that
  630. a highly isolated transaction will return the same values as were
  631. previously read in that same transaction, regardless of changes
  632. in database state outside of that transaction.
  633. To expire individual objects and individual attributes
  634. on those objects, use :meth:`Session.expire`.
  635. The :class:`.Session` object's default behavior is to
  636. expire all state whenever the :meth:`Session.rollback`
  637. or :meth:`Session.commit` methods are called, so that new
  638. state can be loaded for the new transaction. For this reason,
  639. calling :meth:`Session.expire_all` is not usually needed,
  640. assuming the transaction is isolated.
  641. .. seealso::
  642. :ref:`session_expire` - introductory material
  643. :meth:`.Session.expire`
  644. :meth:`.Session.refresh`
  645. :meth:`_orm.Query.populate_existing`
  646. """ # noqa: E501
  647. return self._proxied.expire_all()
  648. def expunge(self, instance: object) -> None:
  649. r"""Remove the `instance` from this ``Session``.
  650. .. container:: class_bases
  651. Proxied for the :class:`_orm.Session` class on
  652. behalf of the :class:`_orm.scoping.scoped_session` class.
  653. This will free all internal references to the instance. Cascading
  654. will be applied according to the *expunge* cascade rule.
  655. """ # noqa: E501
  656. return self._proxied.expunge(instance)
  657. def expunge_all(self) -> None:
  658. r"""Remove all object instances from this ``Session``.
  659. .. container:: class_bases
  660. Proxied for the :class:`_orm.Session` class on
  661. behalf of the :class:`_orm.scoping.scoped_session` class.
  662. This is equivalent to calling ``expunge(obj)`` on all objects in this
  663. ``Session``.
  664. """ # noqa: E501
  665. return self._proxied.expunge_all()
  666. def flush(self, objects: Optional[Sequence[Any]] = None) -> None:
  667. r"""Flush all the object changes to the database.
  668. .. container:: class_bases
  669. Proxied for the :class:`_orm.Session` class on
  670. behalf of the :class:`_orm.scoping.scoped_session` class.
  671. Writes out all pending object creations, deletions and modifications
  672. to the database as INSERTs, DELETEs, UPDATEs, etc. Operations are
  673. automatically ordered by the Session's unit of work dependency
  674. solver.
  675. Database operations will be issued in the current transactional
  676. context and do not affect the state of the transaction, unless an
  677. error occurs, in which case the entire transaction is rolled back.
  678. You may flush() as often as you like within a transaction to move
  679. changes from Python to the database's transaction buffer.
  680. :param objects: Optional; restricts the flush operation to operate
  681. only on elements that are in the given collection.
  682. This feature is for an extremely narrow set of use cases where
  683. particular objects may need to be operated upon before the
  684. full flush() occurs. It is not intended for general use.
  685. """ # noqa: E501
  686. return self._proxied.flush(objects=objects)
  687. def get(
  688. self,
  689. entity: _EntityBindKey[_O],
  690. ident: _PKIdentityArgument,
  691. *,
  692. options: Optional[Sequence[ORMOption]] = None,
  693. populate_existing: bool = False,
  694. with_for_update: ForUpdateParameter = None,
  695. identity_token: Optional[Any] = None,
  696. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  697. bind_arguments: Optional[_BindArguments] = None,
  698. ) -> Optional[_O]:
  699. r"""Return an instance based on the given primary key identifier,
  700. or ``None`` if not found.
  701. .. container:: class_bases
  702. Proxied for the :class:`_orm.Session` class on
  703. behalf of the :class:`_orm.scoping.scoped_session` class.
  704. E.g.::
  705. my_user = session.get(User, 5)
  706. some_object = session.get(VersionedFoo, (5, 10))
  707. some_object = session.get(VersionedFoo, {"id": 5, "version_id": 10})
  708. .. versionadded:: 1.4 Added :meth:`_orm.Session.get`, which is moved
  709. from the now legacy :meth:`_orm.Query.get` method.
  710. :meth:`_orm.Session.get` is special in that it provides direct
  711. access to the identity map of the :class:`.Session`.
  712. If the given primary key identifier is present
  713. in the local identity map, the object is returned
  714. directly from this collection and no SQL is emitted,
  715. unless the object has been marked fully expired.
  716. If not present,
  717. a SELECT is performed in order to locate the object.
  718. :meth:`_orm.Session.get` also will perform a check if
  719. the object is present in the identity map and
  720. marked as expired - a SELECT
  721. is emitted to refresh the object as well as to
  722. ensure that the row is still present.
  723. If not, :class:`~sqlalchemy.orm.exc.ObjectDeletedError` is raised.
  724. :param entity: a mapped class or :class:`.Mapper` indicating the
  725. type of entity to be loaded.
  726. :param ident: A scalar, tuple, or dictionary representing the
  727. primary key. For a composite (e.g. multiple column) primary key,
  728. a tuple or dictionary should be passed.
  729. For a single-column primary key, the scalar calling form is typically
  730. the most expedient. If the primary key of a row is the value "5",
  731. the call looks like::
  732. my_object = session.get(SomeClass, 5)
  733. The tuple form contains primary key values typically in
  734. the order in which they correspond to the mapped
  735. :class:`_schema.Table`
  736. object's primary key columns, or if the
  737. :paramref:`_orm.Mapper.primary_key` configuration parameter were
  738. used, in
  739. the order used for that parameter. For example, if the primary key
  740. of a row is represented by the integer
  741. digits "5, 10" the call would look like::
  742. my_object = session.get(SomeClass, (5, 10))
  743. The dictionary form should include as keys the mapped attribute names
  744. corresponding to each element of the primary key. If the mapped class
  745. has the attributes ``id``, ``version_id`` as the attributes which
  746. store the object's primary key value, the call would look like::
  747. my_object = session.get(SomeClass, {"id": 5, "version_id": 10})
  748. :param options: optional sequence of loader options which will be
  749. applied to the query, if one is emitted.
  750. :param populate_existing: causes the method to unconditionally emit
  751. a SQL query and refresh the object with the newly loaded data,
  752. regardless of whether or not the object is already present.
  753. :param with_for_update: optional boolean ``True`` indicating FOR UPDATE
  754. should be used, or may be a dictionary containing flags to
  755. indicate a more specific set of FOR UPDATE flags for the SELECT;
  756. flags should match the parameters of
  757. :meth:`_query.Query.with_for_update`.
  758. Supersedes the :paramref:`.Session.refresh.lockmode` parameter.
  759. :param execution_options: optional dictionary of execution options,
  760. which will be associated with the query execution if one is emitted.
  761. This dictionary can provide a subset of the options that are
  762. accepted by :meth:`_engine.Connection.execution_options`, and may
  763. also provide additional options understood only in an ORM context.
  764. .. versionadded:: 1.4.29
  765. .. seealso::
  766. :ref:`orm_queryguide_execution_options` - ORM-specific execution
  767. options
  768. :param bind_arguments: dictionary of additional arguments to determine
  769. the bind. May include "mapper", "bind", or other custom arguments.
  770. Contents of this dictionary are passed to the
  771. :meth:`.Session.get_bind` method.
  772. .. versionadded: 2.0.0rc1
  773. :return: The object instance, or ``None``.
  774. """ # noqa: E501
  775. return self._proxied.get(
  776. entity,
  777. ident,
  778. options=options,
  779. populate_existing=populate_existing,
  780. with_for_update=with_for_update,
  781. identity_token=identity_token,
  782. execution_options=execution_options,
  783. bind_arguments=bind_arguments,
  784. )
  785. def get_one(
  786. self,
  787. entity: _EntityBindKey[_O],
  788. ident: _PKIdentityArgument,
  789. *,
  790. options: Optional[Sequence[ORMOption]] = None,
  791. populate_existing: bool = False,
  792. with_for_update: ForUpdateParameter = None,
  793. identity_token: Optional[Any] = None,
  794. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  795. bind_arguments: Optional[_BindArguments] = None,
  796. ) -> _O:
  797. r"""Return exactly one instance based on the given primary key
  798. identifier, or raise an exception if not found.
  799. .. container:: class_bases
  800. Proxied for the :class:`_orm.Session` class on
  801. behalf of the :class:`_orm.scoping.scoped_session` class.
  802. Raises :class:`_exc.NoResultFound` if the query selects no rows.
  803. For a detailed documentation of the arguments see the
  804. method :meth:`.Session.get`.
  805. .. versionadded:: 2.0.22
  806. :return: The object instance.
  807. .. seealso::
  808. :meth:`.Session.get` - equivalent method that instead
  809. returns ``None`` if no row was found with the provided primary
  810. key
  811. """ # noqa: E501
  812. return self._proxied.get_one(
  813. entity,
  814. ident,
  815. options=options,
  816. populate_existing=populate_existing,
  817. with_for_update=with_for_update,
  818. identity_token=identity_token,
  819. execution_options=execution_options,
  820. bind_arguments=bind_arguments,
  821. )
  822. def get_bind(
  823. self,
  824. mapper: Optional[_EntityBindKey[_O]] = None,
  825. *,
  826. clause: Optional[ClauseElement] = None,
  827. bind: Optional[_SessionBind] = None,
  828. _sa_skip_events: Optional[bool] = None,
  829. _sa_skip_for_implicit_returning: bool = False,
  830. **kw: Any,
  831. ) -> Union[Engine, Connection]:
  832. r"""Return a "bind" to which this :class:`.Session` is bound.
  833. .. container:: class_bases
  834. Proxied for the :class:`_orm.Session` class on
  835. behalf of the :class:`_orm.scoping.scoped_session` class.
  836. The "bind" is usually an instance of :class:`_engine.Engine`,
  837. except in the case where the :class:`.Session` has been
  838. explicitly bound directly to a :class:`_engine.Connection`.
  839. For a multiply-bound or unbound :class:`.Session`, the
  840. ``mapper`` or ``clause`` arguments are used to determine the
  841. appropriate bind to return.
  842. Note that the "mapper" argument is usually present
  843. when :meth:`.Session.get_bind` is called via an ORM
  844. operation such as a :meth:`.Session.query`, each
  845. individual INSERT/UPDATE/DELETE operation within a
  846. :meth:`.Session.flush`, call, etc.
  847. The order of resolution is:
  848. 1. if mapper given and :paramref:`.Session.binds` is present,
  849. locate a bind based first on the mapper in use, then
  850. on the mapped class in use, then on any base classes that are
  851. present in the ``__mro__`` of the mapped class, from more specific
  852. superclasses to more general.
  853. 2. if clause given and ``Session.binds`` is present,
  854. locate a bind based on :class:`_schema.Table` objects
  855. found in the given clause present in ``Session.binds``.
  856. 3. if ``Session.binds`` is present, return that.
  857. 4. if clause given, attempt to return a bind
  858. linked to the :class:`_schema.MetaData` ultimately
  859. associated with the clause.
  860. 5. if mapper given, attempt to return a bind
  861. linked to the :class:`_schema.MetaData` ultimately
  862. associated with the :class:`_schema.Table` or other
  863. selectable to which the mapper is mapped.
  864. 6. No bind can be found, :exc:`~sqlalchemy.exc.UnboundExecutionError`
  865. is raised.
  866. Note that the :meth:`.Session.get_bind` method can be overridden on
  867. a user-defined subclass of :class:`.Session` to provide any kind
  868. of bind resolution scheme. See the example at
  869. :ref:`session_custom_partitioning`.
  870. :param mapper:
  871. Optional mapped class or corresponding :class:`_orm.Mapper` instance.
  872. The bind can be derived from a :class:`_orm.Mapper` first by
  873. consulting the "binds" map associated with this :class:`.Session`,
  874. and secondly by consulting the :class:`_schema.MetaData` associated
  875. with the :class:`_schema.Table` to which the :class:`_orm.Mapper` is
  876. mapped for a bind.
  877. :param clause:
  878. A :class:`_expression.ClauseElement` (i.e.
  879. :func:`_expression.select`,
  880. :func:`_expression.text`,
  881. etc.). If the ``mapper`` argument is not present or could not
  882. produce a bind, the given expression construct will be searched
  883. for a bound element, typically a :class:`_schema.Table`
  884. associated with
  885. bound :class:`_schema.MetaData`.
  886. .. seealso::
  887. :ref:`session_partitioning`
  888. :paramref:`.Session.binds`
  889. :meth:`.Session.bind_mapper`
  890. :meth:`.Session.bind_table`
  891. """ # noqa: E501
  892. return self._proxied.get_bind(
  893. mapper=mapper,
  894. clause=clause,
  895. bind=bind,
  896. _sa_skip_events=_sa_skip_events,
  897. _sa_skip_for_implicit_returning=_sa_skip_for_implicit_returning,
  898. **kw,
  899. )
  900. def is_modified(
  901. self, instance: object, include_collections: bool = True
  902. ) -> bool:
  903. r"""Return ``True`` if the given instance has locally
  904. modified attributes.
  905. .. container:: class_bases
  906. Proxied for the :class:`_orm.Session` class on
  907. behalf of the :class:`_orm.scoping.scoped_session` class.
  908. This method retrieves the history for each instrumented
  909. attribute on the instance and performs a comparison of the current
  910. value to its previously flushed or committed value, if any.
  911. It is in effect a more expensive and accurate
  912. version of checking for the given instance in the
  913. :attr:`.Session.dirty` collection; a full test for
  914. each attribute's net "dirty" status is performed.
  915. E.g.::
  916. return session.is_modified(someobject)
  917. A few caveats to this method apply:
  918. * Instances present in the :attr:`.Session.dirty` collection may
  919. report ``False`` when tested with this method. This is because
  920. the object may have received change events via attribute mutation,
  921. thus placing it in :attr:`.Session.dirty`, but ultimately the state
  922. is the same as that loaded from the database, resulting in no net
  923. change here.
  924. * Scalar attributes may not have recorded the previously set
  925. value when a new value was applied, if the attribute was not loaded,
  926. or was expired, at the time the new value was received - in these
  927. cases, the attribute is assumed to have a change, even if there is
  928. ultimately no net change against its database value. SQLAlchemy in
  929. most cases does not need the "old" value when a set event occurs, so
  930. it skips the expense of a SQL call if the old value isn't present,
  931. based on the assumption that an UPDATE of the scalar value is
  932. usually needed, and in those few cases where it isn't, is less
  933. expensive on average than issuing a defensive SELECT.
  934. The "old" value is fetched unconditionally upon set only if the
  935. attribute container has the ``active_history`` flag set to ``True``.
  936. This flag is set typically for primary key attributes and scalar
  937. object references that are not a simple many-to-one. To set this
  938. flag for any arbitrary mapped column, use the ``active_history``
  939. argument with :func:`.column_property`.
  940. :param instance: mapped instance to be tested for pending changes.
  941. :param include_collections: Indicates if multivalued collections
  942. should be included in the operation. Setting this to ``False`` is a
  943. way to detect only local-column based properties (i.e. scalar columns
  944. or many-to-one foreign keys) that would result in an UPDATE for this
  945. instance upon flush.
  946. """ # noqa: E501
  947. return self._proxied.is_modified(
  948. instance, include_collections=include_collections
  949. )
  950. def bulk_save_objects(
  951. self,
  952. objects: Iterable[object],
  953. return_defaults: bool = False,
  954. update_changed_only: bool = True,
  955. preserve_order: bool = True,
  956. ) -> None:
  957. r"""Perform a bulk save of the given list of objects.
  958. .. container:: class_bases
  959. Proxied for the :class:`_orm.Session` class on
  960. behalf of the :class:`_orm.scoping.scoped_session` class.
  961. .. legacy::
  962. This method is a legacy feature as of the 2.0 series of
  963. SQLAlchemy. For modern bulk INSERT and UPDATE, see
  964. the sections :ref:`orm_queryguide_bulk_insert` and
  965. :ref:`orm_queryguide_bulk_update`.
  966. For general INSERT and UPDATE of existing ORM mapped objects,
  967. prefer standard :term:`unit of work` data management patterns,
  968. introduced in the :ref:`unified_tutorial` at
  969. :ref:`tutorial_orm_data_manipulation`. SQLAlchemy 2.0
  970. now uses :ref:`engine_insertmanyvalues` with modern dialects
  971. which solves previous issues of bulk INSERT slowness.
  972. :param objects: a sequence of mapped object instances. The mapped
  973. objects are persisted as is, and are **not** associated with the
  974. :class:`.Session` afterwards.
  975. For each object, whether the object is sent as an INSERT or an
  976. UPDATE is dependent on the same rules used by the :class:`.Session`
  977. in traditional operation; if the object has the
  978. :attr:`.InstanceState.key`
  979. attribute set, then the object is assumed to be "detached" and
  980. will result in an UPDATE. Otherwise, an INSERT is used.
  981. In the case of an UPDATE, statements are grouped based on which
  982. attributes have changed, and are thus to be the subject of each
  983. SET clause. If ``update_changed_only`` is False, then all
  984. attributes present within each object are applied to the UPDATE
  985. statement, which may help in allowing the statements to be grouped
  986. together into a larger executemany(), and will also reduce the
  987. overhead of checking history on attributes.
  988. :param return_defaults: when True, rows that are missing values which
  989. generate defaults, namely integer primary key defaults and sequences,
  990. will be inserted **one at a time**, so that the primary key value
  991. is available. In particular this will allow joined-inheritance
  992. and other multi-table mappings to insert correctly without the need
  993. to provide primary key values ahead of time; however,
  994. :paramref:`.Session.bulk_save_objects.return_defaults` **greatly
  995. reduces the performance gains** of the method overall. It is strongly
  996. advised to please use the standard :meth:`_orm.Session.add_all`
  997. approach.
  998. :param update_changed_only: when True, UPDATE statements are rendered
  999. based on those attributes in each state that have logged changes.
  1000. When False, all attributes present are rendered into the SET clause
  1001. with the exception of primary key attributes.
  1002. :param preserve_order: when True, the order of inserts and updates
  1003. matches exactly the order in which the objects are given. When
  1004. False, common types of objects are grouped into inserts
  1005. and updates, to allow for more batching opportunities.
  1006. .. seealso::
  1007. :doc:`queryguide/dml`
  1008. :meth:`.Session.bulk_insert_mappings`
  1009. :meth:`.Session.bulk_update_mappings`
  1010. """ # noqa: E501
  1011. return self._proxied.bulk_save_objects(
  1012. objects,
  1013. return_defaults=return_defaults,
  1014. update_changed_only=update_changed_only,
  1015. preserve_order=preserve_order,
  1016. )
  1017. def bulk_insert_mappings(
  1018. self,
  1019. mapper: Mapper[Any],
  1020. mappings: Iterable[Dict[str, Any]],
  1021. return_defaults: bool = False,
  1022. render_nulls: bool = False,
  1023. ) -> None:
  1024. r"""Perform a bulk insert of the given list of mapping dictionaries.
  1025. .. container:: class_bases
  1026. Proxied for the :class:`_orm.Session` class on
  1027. behalf of the :class:`_orm.scoping.scoped_session` class.
  1028. .. legacy::
  1029. This method is a legacy feature as of the 2.0 series of
  1030. SQLAlchemy. For modern bulk INSERT and UPDATE, see
  1031. the sections :ref:`orm_queryguide_bulk_insert` and
  1032. :ref:`orm_queryguide_bulk_update`. The 2.0 API shares
  1033. implementation details with this method and adds new features
  1034. as well.
  1035. :param mapper: a mapped class, or the actual :class:`_orm.Mapper`
  1036. object,
  1037. representing the single kind of object represented within the mapping
  1038. list.
  1039. :param mappings: a sequence of dictionaries, each one containing the
  1040. state of the mapped row to be inserted, in terms of the attribute
  1041. names on the mapped class. If the mapping refers to multiple tables,
  1042. such as a joined-inheritance mapping, each dictionary must contain all
  1043. keys to be populated into all tables.
  1044. :param return_defaults: when True, the INSERT process will be altered
  1045. to ensure that newly generated primary key values will be fetched.
  1046. The rationale for this parameter is typically to enable
  1047. :ref:`Joined Table Inheritance <joined_inheritance>` mappings to
  1048. be bulk inserted.
  1049. .. note:: for backends that don't support RETURNING, the
  1050. :paramref:`_orm.Session.bulk_insert_mappings.return_defaults`
  1051. parameter can significantly decrease performance as INSERT
  1052. statements can no longer be batched. See
  1053. :ref:`engine_insertmanyvalues`
  1054. for background on which backends are affected.
  1055. :param render_nulls: When True, a value of ``None`` will result
  1056. in a NULL value being included in the INSERT statement, rather
  1057. than the column being omitted from the INSERT. This allows all
  1058. the rows being INSERTed to have the identical set of columns which
  1059. allows the full set of rows to be batched to the DBAPI. Normally,
  1060. each column-set that contains a different combination of NULL values
  1061. than the previous row must omit a different series of columns from
  1062. the rendered INSERT statement, which means it must be emitted as a
  1063. separate statement. By passing this flag, the full set of rows
  1064. are guaranteed to be batchable into one batch; the cost however is
  1065. that server-side defaults which are invoked by an omitted column will
  1066. be skipped, so care must be taken to ensure that these are not
  1067. necessary.
  1068. .. warning::
  1069. When this flag is set, **server side default SQL values will
  1070. not be invoked** for those columns that are inserted as NULL;
  1071. the NULL value will be sent explicitly. Care must be taken
  1072. to ensure that no server-side default functions need to be
  1073. invoked for the operation as a whole.
  1074. .. seealso::
  1075. :doc:`queryguide/dml`
  1076. :meth:`.Session.bulk_save_objects`
  1077. :meth:`.Session.bulk_update_mappings`
  1078. """ # noqa: E501
  1079. return self._proxied.bulk_insert_mappings(
  1080. mapper,
  1081. mappings,
  1082. return_defaults=return_defaults,
  1083. render_nulls=render_nulls,
  1084. )
  1085. def bulk_update_mappings(
  1086. self, mapper: Mapper[Any], mappings: Iterable[Dict[str, Any]]
  1087. ) -> None:
  1088. r"""Perform a bulk update of the given list of mapping dictionaries.
  1089. .. container:: class_bases
  1090. Proxied for the :class:`_orm.Session` class on
  1091. behalf of the :class:`_orm.scoping.scoped_session` class.
  1092. .. legacy::
  1093. This method is a legacy feature as of the 2.0 series of
  1094. SQLAlchemy. For modern bulk INSERT and UPDATE, see
  1095. the sections :ref:`orm_queryguide_bulk_insert` and
  1096. :ref:`orm_queryguide_bulk_update`. The 2.0 API shares
  1097. implementation details with this method and adds new features
  1098. as well.
  1099. :param mapper: a mapped class, or the actual :class:`_orm.Mapper`
  1100. object,
  1101. representing the single kind of object represented within the mapping
  1102. list.
  1103. :param mappings: a sequence of dictionaries, each one containing the
  1104. state of the mapped row to be updated, in terms of the attribute names
  1105. on the mapped class. If the mapping refers to multiple tables, such
  1106. as a joined-inheritance mapping, each dictionary may contain keys
  1107. corresponding to all tables. All those keys which are present and
  1108. are not part of the primary key are applied to the SET clause of the
  1109. UPDATE statement; the primary key values, which are required, are
  1110. applied to the WHERE clause.
  1111. .. seealso::
  1112. :doc:`queryguide/dml`
  1113. :meth:`.Session.bulk_insert_mappings`
  1114. :meth:`.Session.bulk_save_objects`
  1115. """ # noqa: E501
  1116. return self._proxied.bulk_update_mappings(mapper, mappings)
  1117. def merge(
  1118. self,
  1119. instance: _O,
  1120. *,
  1121. load: bool = True,
  1122. options: Optional[Sequence[ORMOption]] = None,
  1123. ) -> _O:
  1124. r"""Copy the state of a given instance into a corresponding instance
  1125. within this :class:`.Session`.
  1126. .. container:: class_bases
  1127. Proxied for the :class:`_orm.Session` class on
  1128. behalf of the :class:`_orm.scoping.scoped_session` class.
  1129. :meth:`.Session.merge` examines the primary key attributes of the
  1130. source instance, and attempts to reconcile it with an instance of the
  1131. same primary key in the session. If not found locally, it attempts
  1132. to load the object from the database based on primary key, and if
  1133. none can be located, creates a new instance. The state of each
  1134. attribute on the source instance is then copied to the target
  1135. instance. The resulting target instance is then returned by the
  1136. method; the original source instance is left unmodified, and
  1137. un-associated with the :class:`.Session` if not already.
  1138. This operation cascades to associated instances if the association is
  1139. mapped with ``cascade="merge"``.
  1140. See :ref:`unitofwork_merging` for a detailed discussion of merging.
  1141. :param instance: Instance to be merged.
  1142. :param load: Boolean, when False, :meth:`.merge` switches into
  1143. a "high performance" mode which causes it to forego emitting history
  1144. events as well as all database access. This flag is used for
  1145. cases such as transferring graphs of objects into a :class:`.Session`
  1146. from a second level cache, or to transfer just-loaded objects
  1147. into the :class:`.Session` owned by a worker thread or process
  1148. without re-querying the database.
  1149. The ``load=False`` use case adds the caveat that the given
  1150. object has to be in a "clean" state, that is, has no pending changes
  1151. to be flushed - even if the incoming object is detached from any
  1152. :class:`.Session`. This is so that when
  1153. the merge operation populates local attributes and
  1154. cascades to related objects and
  1155. collections, the values can be "stamped" onto the
  1156. target object as is, without generating any history or attribute
  1157. events, and without the need to reconcile the incoming data with
  1158. any existing related objects or collections that might not
  1159. be loaded. The resulting objects from ``load=False`` are always
  1160. produced as "clean", so it is only appropriate that the given objects
  1161. should be "clean" as well, else this suggests a mis-use of the
  1162. method.
  1163. :param options: optional sequence of loader options which will be
  1164. applied to the :meth:`_orm.Session.get` method when the merge
  1165. operation loads the existing version of the object from the database.
  1166. .. versionadded:: 1.4.24
  1167. .. seealso::
  1168. :func:`.make_transient_to_detached` - provides for an alternative
  1169. means of "merging" a single object into the :class:`.Session`
  1170. """ # noqa: E501
  1171. return self._proxied.merge(instance, load=load, options=options)
  1172. @overload
  1173. def query(self, _entity: _EntityType[_O]) -> Query[_O]: ...
  1174. @overload
  1175. def query(
  1176. self, _colexpr: TypedColumnsClauseRole[_T]
  1177. ) -> RowReturningQuery[Tuple[_T]]: ...
  1178. # START OVERLOADED FUNCTIONS self.query RowReturningQuery 2-8
  1179. # code within this block is **programmatically,
  1180. # statically generated** by tools/generate_tuple_map_overloads.py
  1181. @overload
  1182. def query(
  1183. self, __ent0: _TCCA[_T0], __ent1: _TCCA[_T1]
  1184. ) -> RowReturningQuery[Tuple[_T0, _T1]]: ...
  1185. @overload
  1186. def query(
  1187. self, __ent0: _TCCA[_T0], __ent1: _TCCA[_T1], __ent2: _TCCA[_T2]
  1188. ) -> RowReturningQuery[Tuple[_T0, _T1, _T2]]: ...
  1189. @overload
  1190. def query(
  1191. self,
  1192. __ent0: _TCCA[_T0],
  1193. __ent1: _TCCA[_T1],
  1194. __ent2: _TCCA[_T2],
  1195. __ent3: _TCCA[_T3],
  1196. ) -> RowReturningQuery[Tuple[_T0, _T1, _T2, _T3]]: ...
  1197. @overload
  1198. def query(
  1199. self,
  1200. __ent0: _TCCA[_T0],
  1201. __ent1: _TCCA[_T1],
  1202. __ent2: _TCCA[_T2],
  1203. __ent3: _TCCA[_T3],
  1204. __ent4: _TCCA[_T4],
  1205. ) -> RowReturningQuery[Tuple[_T0, _T1, _T2, _T3, _T4]]: ...
  1206. @overload
  1207. def query(
  1208. self,
  1209. __ent0: _TCCA[_T0],
  1210. __ent1: _TCCA[_T1],
  1211. __ent2: _TCCA[_T2],
  1212. __ent3: _TCCA[_T3],
  1213. __ent4: _TCCA[_T4],
  1214. __ent5: _TCCA[_T5],
  1215. ) -> RowReturningQuery[Tuple[_T0, _T1, _T2, _T3, _T4, _T5]]: ...
  1216. @overload
  1217. def query(
  1218. self,
  1219. __ent0: _TCCA[_T0],
  1220. __ent1: _TCCA[_T1],
  1221. __ent2: _TCCA[_T2],
  1222. __ent3: _TCCA[_T3],
  1223. __ent4: _TCCA[_T4],
  1224. __ent5: _TCCA[_T5],
  1225. __ent6: _TCCA[_T6],
  1226. ) -> RowReturningQuery[Tuple[_T0, _T1, _T2, _T3, _T4, _T5, _T6]]: ...
  1227. @overload
  1228. def query(
  1229. self,
  1230. __ent0: _TCCA[_T0],
  1231. __ent1: _TCCA[_T1],
  1232. __ent2: _TCCA[_T2],
  1233. __ent3: _TCCA[_T3],
  1234. __ent4: _TCCA[_T4],
  1235. __ent5: _TCCA[_T5],
  1236. __ent6: _TCCA[_T6],
  1237. __ent7: _TCCA[_T7],
  1238. ) -> RowReturningQuery[Tuple[_T0, _T1, _T2, _T3, _T4, _T5, _T6, _T7]]: ...
  1239. # END OVERLOADED FUNCTIONS self.query
  1240. @overload
  1241. def query(
  1242. self, *entities: _ColumnsClauseArgument[Any], **kwargs: Any
  1243. ) -> Query[Any]: ...
  1244. def query(
  1245. self, *entities: _ColumnsClauseArgument[Any], **kwargs: Any
  1246. ) -> Query[Any]:
  1247. r"""Return a new :class:`_query.Query` object corresponding to this
  1248. :class:`_orm.Session`.
  1249. .. container:: class_bases
  1250. Proxied for the :class:`_orm.Session` class on
  1251. behalf of the :class:`_orm.scoping.scoped_session` class.
  1252. Note that the :class:`_query.Query` object is legacy as of
  1253. SQLAlchemy 2.0; the :func:`_sql.select` construct is now used
  1254. to construct ORM queries.
  1255. .. seealso::
  1256. :ref:`unified_tutorial`
  1257. :ref:`queryguide_toplevel`
  1258. :ref:`query_api_toplevel` - legacy API doc
  1259. """ # noqa: E501
  1260. return self._proxied.query(*entities, **kwargs)
  1261. def refresh(
  1262. self,
  1263. instance: object,
  1264. attribute_names: Optional[Iterable[str]] = None,
  1265. with_for_update: ForUpdateParameter = None,
  1266. ) -> None:
  1267. r"""Expire and refresh attributes on the given instance.
  1268. .. container:: class_bases
  1269. Proxied for the :class:`_orm.Session` class on
  1270. behalf of the :class:`_orm.scoping.scoped_session` class.
  1271. The selected attributes will first be expired as they would when using
  1272. :meth:`_orm.Session.expire`; then a SELECT statement will be issued to
  1273. the database to refresh column-oriented attributes with the current
  1274. value available in the current transaction.
  1275. :func:`_orm.relationship` oriented attributes will also be immediately
  1276. loaded if they were already eagerly loaded on the object, using the
  1277. same eager loading strategy that they were loaded with originally.
  1278. .. versionadded:: 1.4 - the :meth:`_orm.Session.refresh` method
  1279. can also refresh eagerly loaded attributes.
  1280. :func:`_orm.relationship` oriented attributes that would normally
  1281. load using the ``select`` (or "lazy") loader strategy will also
  1282. load **if they are named explicitly in the attribute_names
  1283. collection**, emitting a SELECT statement for the attribute using the
  1284. ``immediate`` loader strategy. If lazy-loaded relationships are not
  1285. named in :paramref:`_orm.Session.refresh.attribute_names`, then
  1286. they remain as "lazy loaded" attributes and are not implicitly
  1287. refreshed.
  1288. .. versionchanged:: 2.0.4 The :meth:`_orm.Session.refresh` method
  1289. will now refresh lazy-loaded :func:`_orm.relationship` oriented
  1290. attributes for those which are named explicitly in the
  1291. :paramref:`_orm.Session.refresh.attribute_names` collection.
  1292. .. tip::
  1293. While the :meth:`_orm.Session.refresh` method is capable of
  1294. refreshing both column and relationship oriented attributes, its
  1295. primary focus is on refreshing of local column-oriented attributes
  1296. on a single instance. For more open ended "refresh" functionality,
  1297. including the ability to refresh the attributes on many objects at
  1298. once while having explicit control over relationship loader
  1299. strategies, use the
  1300. :ref:`populate existing <orm_queryguide_populate_existing>` feature
  1301. instead.
  1302. Note that a highly isolated transaction will return the same values as
  1303. were previously read in that same transaction, regardless of changes
  1304. in database state outside of that transaction. Refreshing
  1305. attributes usually only makes sense at the start of a transaction
  1306. where database rows have not yet been accessed.
  1307. :param attribute_names: optional. An iterable collection of
  1308. string attribute names indicating a subset of attributes to
  1309. be refreshed.
  1310. :param with_for_update: optional boolean ``True`` indicating FOR UPDATE
  1311. should be used, or may be a dictionary containing flags to
  1312. indicate a more specific set of FOR UPDATE flags for the SELECT;
  1313. flags should match the parameters of
  1314. :meth:`_query.Query.with_for_update`.
  1315. Supersedes the :paramref:`.Session.refresh.lockmode` parameter.
  1316. .. seealso::
  1317. :ref:`session_expire` - introductory material
  1318. :meth:`.Session.expire`
  1319. :meth:`.Session.expire_all`
  1320. :ref:`orm_queryguide_populate_existing` - allows any ORM query
  1321. to refresh objects as they would be loaded normally.
  1322. """ # noqa: E501
  1323. return self._proxied.refresh(
  1324. instance,
  1325. attribute_names=attribute_names,
  1326. with_for_update=with_for_update,
  1327. )
  1328. def rollback(self) -> None:
  1329. r"""Rollback the current transaction in progress.
  1330. .. container:: class_bases
  1331. Proxied for the :class:`_orm.Session` class on
  1332. behalf of the :class:`_orm.scoping.scoped_session` class.
  1333. If no transaction is in progress, this method is a pass-through.
  1334. The method always rolls back
  1335. the topmost database transaction, discarding any nested
  1336. transactions that may be in progress.
  1337. .. seealso::
  1338. :ref:`session_rollback`
  1339. :ref:`unitofwork_transaction`
  1340. """ # noqa: E501
  1341. return self._proxied.rollback()
  1342. @overload
  1343. def scalar(
  1344. self,
  1345. statement: TypedReturnsRows[Tuple[_T]],
  1346. params: Optional[_CoreSingleExecuteParams] = None,
  1347. *,
  1348. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  1349. bind_arguments: Optional[_BindArguments] = None,
  1350. **kw: Any,
  1351. ) -> Optional[_T]: ...
  1352. @overload
  1353. def scalar(
  1354. self,
  1355. statement: Executable,
  1356. params: Optional[_CoreSingleExecuteParams] = None,
  1357. *,
  1358. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  1359. bind_arguments: Optional[_BindArguments] = None,
  1360. **kw: Any,
  1361. ) -> Any: ...
  1362. def scalar(
  1363. self,
  1364. statement: Executable,
  1365. params: Optional[_CoreSingleExecuteParams] = None,
  1366. *,
  1367. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  1368. bind_arguments: Optional[_BindArguments] = None,
  1369. **kw: Any,
  1370. ) -> Any:
  1371. r"""Execute a statement and return a scalar result.
  1372. .. container:: class_bases
  1373. Proxied for the :class:`_orm.Session` class on
  1374. behalf of the :class:`_orm.scoping.scoped_session` class.
  1375. Usage and parameters are the same as that of
  1376. :meth:`_orm.Session.execute`; the return result is a scalar Python
  1377. value.
  1378. """ # noqa: E501
  1379. return self._proxied.scalar(
  1380. statement,
  1381. params=params,
  1382. execution_options=execution_options,
  1383. bind_arguments=bind_arguments,
  1384. **kw,
  1385. )
  1386. @overload
  1387. def scalars(
  1388. self,
  1389. statement: TypedReturnsRows[Tuple[_T]],
  1390. params: Optional[_CoreAnyExecuteParams] = None,
  1391. *,
  1392. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  1393. bind_arguments: Optional[_BindArguments] = None,
  1394. **kw: Any,
  1395. ) -> ScalarResult[_T]: ...
  1396. @overload
  1397. def scalars(
  1398. self,
  1399. statement: Executable,
  1400. params: Optional[_CoreAnyExecuteParams] = None,
  1401. *,
  1402. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  1403. bind_arguments: Optional[_BindArguments] = None,
  1404. **kw: Any,
  1405. ) -> ScalarResult[Any]: ...
  1406. def scalars(
  1407. self,
  1408. statement: Executable,
  1409. params: Optional[_CoreAnyExecuteParams] = None,
  1410. *,
  1411. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  1412. bind_arguments: Optional[_BindArguments] = None,
  1413. **kw: Any,
  1414. ) -> ScalarResult[Any]:
  1415. r"""Execute a statement and return the results as scalars.
  1416. .. container:: class_bases
  1417. Proxied for the :class:`_orm.Session` class on
  1418. behalf of the :class:`_orm.scoping.scoped_session` class.
  1419. Usage and parameters are the same as that of
  1420. :meth:`_orm.Session.execute`; the return result is a
  1421. :class:`_result.ScalarResult` filtering object which
  1422. will return single elements rather than :class:`_row.Row` objects.
  1423. :return: a :class:`_result.ScalarResult` object
  1424. .. versionadded:: 1.4.24 Added :meth:`_orm.Session.scalars`
  1425. .. versionadded:: 1.4.26 Added :meth:`_orm.scoped_session.scalars`
  1426. .. seealso::
  1427. :ref:`orm_queryguide_select_orm_entities` - contrasts the behavior
  1428. of :meth:`_orm.Session.execute` to :meth:`_orm.Session.scalars`
  1429. """ # noqa: E501
  1430. return self._proxied.scalars(
  1431. statement,
  1432. params=params,
  1433. execution_options=execution_options,
  1434. bind_arguments=bind_arguments,
  1435. **kw,
  1436. )
  1437. @property
  1438. def bind(self) -> Optional[Union[Engine, Connection]]:
  1439. r"""Proxy for the :attr:`_orm.Session.bind` attribute
  1440. on behalf of the :class:`_orm.scoping.scoped_session` class.
  1441. """ # noqa: E501
  1442. return self._proxied.bind
  1443. @bind.setter
  1444. def bind(self, attr: Optional[Union[Engine, Connection]]) -> None:
  1445. self._proxied.bind = attr
  1446. @property
  1447. def dirty(self) -> Any:
  1448. r"""The set of all persistent instances considered dirty.
  1449. .. container:: class_bases
  1450. Proxied for the :class:`_orm.Session` class
  1451. on behalf of the :class:`_orm.scoping.scoped_session` class.
  1452. E.g.::
  1453. some_mapped_object in session.dirty
  1454. Instances are considered dirty when they were modified but not
  1455. deleted.
  1456. Note that this 'dirty' calculation is 'optimistic'; most
  1457. attribute-setting or collection modification operations will
  1458. mark an instance as 'dirty' and place it in this set, even if
  1459. there is no net change to the attribute's value. At flush
  1460. time, the value of each attribute is compared to its
  1461. previously saved value, and if there's no net change, no SQL
  1462. operation will occur (this is a more expensive operation so
  1463. it's only done at flush time).
  1464. To check if an instance has actionable net changes to its
  1465. attributes, use the :meth:`.Session.is_modified` method.
  1466. """ # noqa: E501
  1467. return self._proxied.dirty
  1468. @property
  1469. def deleted(self) -> Any:
  1470. r"""The set of all instances marked as 'deleted' within this ``Session``
  1471. .. container:: class_bases
  1472. Proxied for the :class:`_orm.Session` class
  1473. on behalf of the :class:`_orm.scoping.scoped_session` class.
  1474. """ # noqa: E501
  1475. return self._proxied.deleted
  1476. @property
  1477. def new(self) -> Any:
  1478. r"""The set of all instances marked as 'new' within this ``Session``.
  1479. .. container:: class_bases
  1480. Proxied for the :class:`_orm.Session` class
  1481. on behalf of the :class:`_orm.scoping.scoped_session` class.
  1482. """ # noqa: E501
  1483. return self._proxied.new
  1484. @property
  1485. def identity_map(self) -> IdentityMap:
  1486. r"""Proxy for the :attr:`_orm.Session.identity_map` attribute
  1487. on behalf of the :class:`_orm.scoping.scoped_session` class.
  1488. """ # noqa: E501
  1489. return self._proxied.identity_map
  1490. @identity_map.setter
  1491. def identity_map(self, attr: IdentityMap) -> None:
  1492. self._proxied.identity_map = attr
  1493. @property
  1494. def is_active(self) -> Any:
  1495. r"""True if this :class:`.Session` not in "partial rollback" state.
  1496. .. container:: class_bases
  1497. Proxied for the :class:`_orm.Session` class
  1498. on behalf of the :class:`_orm.scoping.scoped_session` class.
  1499. .. versionchanged:: 1.4 The :class:`_orm.Session` no longer begins
  1500. a new transaction immediately, so this attribute will be False
  1501. when the :class:`_orm.Session` is first instantiated.
  1502. "partial rollback" state typically indicates that the flush process
  1503. of the :class:`_orm.Session` has failed, and that the
  1504. :meth:`_orm.Session.rollback` method must be emitted in order to
  1505. fully roll back the transaction.
  1506. If this :class:`_orm.Session` is not in a transaction at all, the
  1507. :class:`_orm.Session` will autobegin when it is first used, so in this
  1508. case :attr:`_orm.Session.is_active` will return True.
  1509. Otherwise, if this :class:`_orm.Session` is within a transaction,
  1510. and that transaction has not been rolled back internally, the
  1511. :attr:`_orm.Session.is_active` will also return True.
  1512. .. seealso::
  1513. :ref:`faq_session_rollback`
  1514. :meth:`_orm.Session.in_transaction`
  1515. """ # noqa: E501
  1516. return self._proxied.is_active
  1517. @property
  1518. def autoflush(self) -> bool:
  1519. r"""Proxy for the :attr:`_orm.Session.autoflush` attribute
  1520. on behalf of the :class:`_orm.scoping.scoped_session` class.
  1521. """ # noqa: E501
  1522. return self._proxied.autoflush
  1523. @autoflush.setter
  1524. def autoflush(self, attr: bool) -> None:
  1525. self._proxied.autoflush = attr
  1526. @property
  1527. def no_autoflush(self) -> Any:
  1528. r"""Return a context manager that disables autoflush.
  1529. .. container:: class_bases
  1530. Proxied for the :class:`_orm.Session` class
  1531. on behalf of the :class:`_orm.scoping.scoped_session` class.
  1532. e.g.::
  1533. with session.no_autoflush:
  1534. some_object = SomeClass()
  1535. session.add(some_object)
  1536. # won't autoflush
  1537. some_object.related_thing = session.query(SomeRelated).first()
  1538. Operations that proceed within the ``with:`` block
  1539. will not be subject to flushes occurring upon query
  1540. access. This is useful when initializing a series
  1541. of objects which involve existing database queries,
  1542. where the uncompleted object should not yet be flushed.
  1543. """ # noqa: E501
  1544. return self._proxied.no_autoflush
  1545. @property
  1546. def info(self) -> Any:
  1547. r"""A user-modifiable dictionary.
  1548. .. container:: class_bases
  1549. Proxied for the :class:`_orm.Session` class
  1550. on behalf of the :class:`_orm.scoping.scoped_session` class.
  1551. The initial value of this dictionary can be populated using the
  1552. ``info`` argument to the :class:`.Session` constructor or
  1553. :class:`.sessionmaker` constructor or factory methods. The dictionary
  1554. here is always local to this :class:`.Session` and can be modified
  1555. independently of all other :class:`.Session` objects.
  1556. """ # noqa: E501
  1557. return self._proxied.info
  1558. @classmethod
  1559. def close_all(cls) -> None:
  1560. r"""Close *all* sessions in memory.
  1561. .. container:: class_bases
  1562. Proxied for the :class:`_orm.Session` class on
  1563. behalf of the :class:`_orm.scoping.scoped_session` class.
  1564. .. deprecated:: 1.3 The :meth:`.Session.close_all` method is deprecated and will be removed in a future release. Please refer to :func:`.session.close_all_sessions`.
  1565. """ # noqa: E501
  1566. return Session.close_all()
  1567. @classmethod
  1568. def object_session(cls, instance: object) -> Optional[Session]:
  1569. r"""Return the :class:`.Session` to which an object belongs.
  1570. .. container:: class_bases
  1571. Proxied for the :class:`_orm.Session` class on
  1572. behalf of the :class:`_orm.scoping.scoped_session` class.
  1573. This is an alias of :func:`.object_session`.
  1574. """ # noqa: E501
  1575. return Session.object_session(instance)
  1576. @classmethod
  1577. def identity_key(
  1578. cls,
  1579. class_: Optional[Type[Any]] = None,
  1580. ident: Union[Any, Tuple[Any, ...]] = None,
  1581. *,
  1582. instance: Optional[Any] = None,
  1583. row: Optional[Union[Row[Any], RowMapping]] = None,
  1584. identity_token: Optional[Any] = None,
  1585. ) -> _IdentityKeyType[Any]:
  1586. r"""Return an identity key.
  1587. .. container:: class_bases
  1588. Proxied for the :class:`_orm.Session` class on
  1589. behalf of the :class:`_orm.scoping.scoped_session` class.
  1590. This is an alias of :func:`.util.identity_key`.
  1591. """ # noqa: E501
  1592. return Session.identity_key(
  1593. class_=class_,
  1594. ident=ident,
  1595. instance=instance,
  1596. row=row,
  1597. identity_token=identity_token,
  1598. )
  1599. # END PROXY METHODS scoped_session
  1600. ScopedSession = scoped_session
  1601. """Old name for backwards compatibility."""