state.py 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. # orm/state.py
  2. # Copyright (C) 2005-2024 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. """Defines instrumentation of instances.
  8. This module is usually not directly visible to user applications, but
  9. defines a large part of the ORM's interactivity.
  10. """
  11. import weakref
  12. from . import base
  13. from . import exc as orm_exc
  14. from . import interfaces
  15. from .base import ATTR_WAS_SET
  16. from .base import INIT_OK
  17. from .base import NEVER_SET
  18. from .base import NO_VALUE
  19. from .base import PASSIVE_NO_INITIALIZE
  20. from .base import PASSIVE_NO_RESULT
  21. from .base import PASSIVE_OFF
  22. from .base import SQL_OK
  23. from .path_registry import PathRegistry
  24. from .. import exc as sa_exc
  25. from .. import inspection
  26. from .. import util
  27. # late-populated by session.py
  28. _sessions = None
  29. # optionally late-provided by sqlalchemy.ext.asyncio.session
  30. _async_provider = None
  31. @inspection._self_inspects
  32. class InstanceState(interfaces.InspectionAttrInfo):
  33. """tracks state information at the instance level.
  34. The :class:`.InstanceState` is a key object used by the
  35. SQLAlchemy ORM in order to track the state of an object;
  36. it is created the moment an object is instantiated, typically
  37. as a result of :term:`instrumentation` which SQLAlchemy applies
  38. to the ``__init__()`` method of the class.
  39. :class:`.InstanceState` is also a semi-public object,
  40. available for runtime inspection as to the state of a
  41. mapped instance, including information such as its current
  42. status within a particular :class:`.Session` and details
  43. about data on individual attributes. The public API
  44. in order to acquire a :class:`.InstanceState` object
  45. is to use the :func:`_sa.inspect` system::
  46. >>> from sqlalchemy import inspect
  47. >>> insp = inspect(some_mapped_object)
  48. >>> insp.attrs.nickname.history
  49. History(added=['new nickname'], unchanged=(), deleted=['nickname'])
  50. .. seealso::
  51. :ref:`orm_mapper_inspection_instancestate`
  52. """
  53. session_id = None
  54. key = None
  55. runid = None
  56. load_options = ()
  57. load_path = PathRegistry.root
  58. insert_order = None
  59. _strong_obj = None
  60. modified = False
  61. expired = False
  62. _deleted = False
  63. _load_pending = False
  64. _orphaned_outside_of_session = False
  65. is_instance = True
  66. identity_token = None
  67. _last_known_values = ()
  68. callables = ()
  69. """A namespace where a per-state loader callable can be associated.
  70. In SQLAlchemy 1.0, this is only used for lazy loaders / deferred
  71. loaders that were set up via query option.
  72. Previously, callables was used also to indicate expired attributes
  73. by storing a link to the InstanceState itself in this dictionary.
  74. This role is now handled by the expired_attributes set.
  75. """
  76. def __init__(self, obj, manager):
  77. self.class_ = obj.__class__
  78. self.manager = manager
  79. self.obj = weakref.ref(obj, self._cleanup)
  80. self.committed_state = {}
  81. self.expired_attributes = set()
  82. expired_attributes = None
  83. """The set of keys which are 'expired' to be loaded by
  84. the manager's deferred scalar loader, assuming no pending
  85. changes.
  86. see also the ``unmodified`` collection which is intersected
  87. against this set when a refresh operation occurs."""
  88. @util.memoized_property
  89. def attrs(self):
  90. """Return a namespace representing each attribute on
  91. the mapped object, including its current value
  92. and history.
  93. The returned object is an instance of :class:`.AttributeState`.
  94. This object allows inspection of the current data
  95. within an attribute as well as attribute history
  96. since the last flush.
  97. """
  98. return util.ImmutableProperties(
  99. dict((key, AttributeState(self, key)) for key in self.manager)
  100. )
  101. @property
  102. def transient(self):
  103. """Return ``True`` if the object is :term:`transient`.
  104. .. seealso::
  105. :ref:`session_object_states`
  106. """
  107. return self.key is None and not self._attached
  108. @property
  109. def pending(self):
  110. """Return ``True`` if the object is :term:`pending`.
  111. .. seealso::
  112. :ref:`session_object_states`
  113. """
  114. return self.key is None and self._attached
  115. @property
  116. def deleted(self):
  117. """Return ``True`` if the object is :term:`deleted`.
  118. An object that is in the deleted state is guaranteed to
  119. not be within the :attr:`.Session.identity_map` of its parent
  120. :class:`.Session`; however if the session's transaction is rolled
  121. back, the object will be restored to the persistent state and
  122. the identity map.
  123. .. note::
  124. The :attr:`.InstanceState.deleted` attribute refers to a specific
  125. state of the object that occurs between the "persistent" and
  126. "detached" states; once the object is :term:`detached`, the
  127. :attr:`.InstanceState.deleted` attribute **no longer returns
  128. True**; in order to detect that a state was deleted, regardless
  129. of whether or not the object is associated with a
  130. :class:`.Session`, use the :attr:`.InstanceState.was_deleted`
  131. accessor.
  132. .. versionadded: 1.1
  133. .. seealso::
  134. :ref:`session_object_states`
  135. """
  136. return self.key is not None and self._attached and self._deleted
  137. @property
  138. def was_deleted(self):
  139. """Return True if this object is or was previously in the
  140. "deleted" state and has not been reverted to persistent.
  141. This flag returns True once the object was deleted in flush.
  142. When the object is expunged from the session either explicitly
  143. or via transaction commit and enters the "detached" state,
  144. this flag will continue to report True.
  145. .. versionadded:: 1.1 - added a local method form of
  146. :func:`.orm.util.was_deleted`.
  147. .. seealso::
  148. :attr:`.InstanceState.deleted` - refers to the "deleted" state
  149. :func:`.orm.util.was_deleted` - standalone function
  150. :ref:`session_object_states`
  151. """
  152. return self._deleted
  153. @property
  154. def persistent(self):
  155. """Return ``True`` if the object is :term:`persistent`.
  156. An object that is in the persistent state is guaranteed to
  157. be within the :attr:`.Session.identity_map` of its parent
  158. :class:`.Session`.
  159. .. versionchanged:: 1.1 The :attr:`.InstanceState.persistent`
  160. accessor no longer returns True for an object that was
  161. "deleted" within a flush; use the :attr:`.InstanceState.deleted`
  162. accessor to detect this state. This allows the "persistent"
  163. state to guarantee membership in the identity map.
  164. .. seealso::
  165. :ref:`session_object_states`
  166. """
  167. return self.key is not None and self._attached and not self._deleted
  168. @property
  169. def detached(self):
  170. """Return ``True`` if the object is :term:`detached`.
  171. .. seealso::
  172. :ref:`session_object_states`
  173. """
  174. return self.key is not None and not self._attached
  175. @property
  176. @util.preload_module("sqlalchemy.orm.session")
  177. def _attached(self):
  178. return (
  179. self.session_id is not None
  180. and self.session_id in util.preloaded.orm_session._sessions
  181. )
  182. def _track_last_known_value(self, key):
  183. """Track the last known value of a particular key after expiration
  184. operations.
  185. .. versionadded:: 1.3
  186. """
  187. if key not in self._last_known_values:
  188. self._last_known_values = dict(self._last_known_values)
  189. self._last_known_values[key] = NO_VALUE
  190. @property
  191. def session(self):
  192. """Return the owning :class:`.Session` for this instance,
  193. or ``None`` if none available.
  194. Note that the result here can in some cases be *different*
  195. from that of ``obj in session``; an object that's been deleted
  196. will report as not ``in session``, however if the transaction is
  197. still in progress, this attribute will still refer to that session.
  198. Only when the transaction is completed does the object become
  199. fully detached under normal circumstances.
  200. .. seealso::
  201. :attr:`_orm.InstanceState.async_session`
  202. """
  203. if self.session_id:
  204. try:
  205. return _sessions[self.session_id]
  206. except KeyError:
  207. pass
  208. return None
  209. @property
  210. def async_session(self):
  211. """Return the owning :class:`_asyncio.AsyncSession` for this instance,
  212. or ``None`` if none available.
  213. This attribute is only non-None when the :mod:`sqlalchemy.ext.asyncio`
  214. API is in use for this ORM object. The returned
  215. :class:`_asyncio.AsyncSession` object will be a proxy for the
  216. :class:`_orm.Session` object that would be returned from the
  217. :attr:`_orm.InstanceState.session` attribute for this
  218. :class:`_orm.InstanceState`.
  219. .. versionadded:: 1.4.18
  220. .. seealso::
  221. :ref:`asyncio_toplevel`
  222. """
  223. if _async_provider is None:
  224. return None
  225. sess = self.session
  226. if sess is not None:
  227. return _async_provider(sess)
  228. else:
  229. return None
  230. @property
  231. def object(self):
  232. """Return the mapped object represented by this
  233. :class:`.InstanceState`."""
  234. return self.obj()
  235. @property
  236. def identity(self):
  237. """Return the mapped identity of the mapped object.
  238. This is the primary key identity as persisted by the ORM
  239. which can always be passed directly to
  240. :meth:`_query.Query.get`.
  241. Returns ``None`` if the object has no primary key identity.
  242. .. note::
  243. An object which is :term:`transient` or :term:`pending`
  244. does **not** have a mapped identity until it is flushed,
  245. even if its attributes include primary key values.
  246. """
  247. if self.key is None:
  248. return None
  249. else:
  250. return self.key[1]
  251. @property
  252. def identity_key(self):
  253. """Return the identity key for the mapped object.
  254. This is the key used to locate the object within
  255. the :attr:`.Session.identity_map` mapping. It contains
  256. the identity as returned by :attr:`.identity` within it.
  257. """
  258. # TODO: just change .key to .identity_key across
  259. # the board ? probably
  260. return self.key
  261. @util.memoized_property
  262. def parents(self):
  263. return {}
  264. @util.memoized_property
  265. def _pending_mutations(self):
  266. return {}
  267. @util.memoized_property
  268. def _empty_collections(self):
  269. return {}
  270. @util.memoized_property
  271. def mapper(self):
  272. """Return the :class:`_orm.Mapper` used for this mapped object."""
  273. return self.manager.mapper
  274. @property
  275. def has_identity(self):
  276. """Return ``True`` if this object has an identity key.
  277. This should always have the same value as the
  278. expression ``state.persistent`` or ``state.detached``.
  279. """
  280. return bool(self.key)
  281. @classmethod
  282. def _detach_states(self, states, session, to_transient=False):
  283. persistent_to_detached = (
  284. session.dispatch.persistent_to_detached or None
  285. )
  286. deleted_to_detached = session.dispatch.deleted_to_detached or None
  287. pending_to_transient = session.dispatch.pending_to_transient or None
  288. persistent_to_transient = (
  289. session.dispatch.persistent_to_transient or None
  290. )
  291. for state in states:
  292. deleted = state._deleted
  293. pending = state.key is None
  294. persistent = not pending and not deleted
  295. state.session_id = None
  296. if to_transient and state.key:
  297. del state.key
  298. if persistent:
  299. if to_transient:
  300. if persistent_to_transient is not None:
  301. persistent_to_transient(session, state)
  302. elif persistent_to_detached is not None:
  303. persistent_to_detached(session, state)
  304. elif deleted and deleted_to_detached is not None:
  305. deleted_to_detached(session, state)
  306. elif pending and pending_to_transient is not None:
  307. pending_to_transient(session, state)
  308. state._strong_obj = None
  309. def _detach(self, session=None):
  310. if session:
  311. InstanceState._detach_states([self], session)
  312. else:
  313. self.session_id = self._strong_obj = None
  314. def _dispose(self):
  315. self._detach()
  316. del self.obj
  317. def _cleanup(self, ref):
  318. """Weakref callback cleanup.
  319. This callable cleans out the state when it is being garbage
  320. collected.
  321. this _cleanup **assumes** that there are no strong refs to us!
  322. Will not work otherwise!
  323. """
  324. # Python builtins become undefined during interpreter shutdown.
  325. # Guard against exceptions during this phase, as the method cannot
  326. # proceed in any case if builtins have been undefined.
  327. if dict is None:
  328. return
  329. instance_dict = self._instance_dict()
  330. if instance_dict is not None:
  331. instance_dict._fast_discard(self)
  332. del self._instance_dict
  333. # we can't possibly be in instance_dict._modified
  334. # b.c. this is weakref cleanup only, that set
  335. # is strong referencing!
  336. # assert self not in instance_dict._modified
  337. self.session_id = self._strong_obj = None
  338. del self.obj
  339. def obj(self):
  340. return None
  341. @property
  342. def dict(self):
  343. """Return the instance dict used by the object.
  344. Under normal circumstances, this is always synonymous
  345. with the ``__dict__`` attribute of the mapped object,
  346. unless an alternative instrumentation system has been
  347. configured.
  348. In the case that the actual object has been garbage
  349. collected, this accessor returns a blank dictionary.
  350. """
  351. o = self.obj()
  352. if o is not None:
  353. return base.instance_dict(o)
  354. else:
  355. return {}
  356. def _initialize_instance(*mixed, **kwargs):
  357. self, instance, args = mixed[0], mixed[1], mixed[2:] # noqa
  358. manager = self.manager
  359. manager.dispatch.init(self, args, kwargs)
  360. try:
  361. return manager.original_init(*mixed[1:], **kwargs)
  362. except:
  363. with util.safe_reraise():
  364. manager.dispatch.init_failure(self, args, kwargs)
  365. def get_history(self, key, passive):
  366. return self.manager[key].impl.get_history(self, self.dict, passive)
  367. def get_impl(self, key):
  368. return self.manager[key].impl
  369. def _get_pending_mutation(self, key):
  370. if key not in self._pending_mutations:
  371. self._pending_mutations[key] = PendingCollection()
  372. return self._pending_mutations[key]
  373. def __getstate__(self):
  374. state_dict = {"instance": self.obj()}
  375. state_dict.update(
  376. (k, self.__dict__[k])
  377. for k in (
  378. "committed_state",
  379. "_pending_mutations",
  380. "modified",
  381. "expired",
  382. "callables",
  383. "key",
  384. "parents",
  385. "load_options",
  386. "class_",
  387. "expired_attributes",
  388. "info",
  389. )
  390. if k in self.__dict__
  391. )
  392. if self.load_path:
  393. state_dict["load_path"] = self.load_path.serialize()
  394. state_dict["manager"] = self.manager._serialize(self, state_dict)
  395. return state_dict
  396. def __setstate__(self, state_dict):
  397. inst = state_dict["instance"]
  398. if inst is not None:
  399. self.obj = weakref.ref(inst, self._cleanup)
  400. self.class_ = inst.__class__
  401. else:
  402. # None being possible here generally new as of 0.7.4
  403. # due to storage of state in "parents". "class_"
  404. # also new.
  405. self.obj = None
  406. self.class_ = state_dict["class_"]
  407. self.committed_state = state_dict.get("committed_state", {})
  408. self._pending_mutations = state_dict.get("_pending_mutations", {})
  409. self.parents = state_dict.get("parents", {})
  410. self.modified = state_dict.get("modified", False)
  411. self.expired = state_dict.get("expired", False)
  412. if "info" in state_dict:
  413. self.info.update(state_dict["info"])
  414. if "callables" in state_dict:
  415. self.callables = state_dict["callables"]
  416. try:
  417. self.expired_attributes = state_dict["expired_attributes"]
  418. except KeyError:
  419. self.expired_attributes = set()
  420. # 0.9 and earlier compat
  421. for k in list(self.callables):
  422. if self.callables[k] is self:
  423. self.expired_attributes.add(k)
  424. del self.callables[k]
  425. else:
  426. if "expired_attributes" in state_dict:
  427. self.expired_attributes = state_dict["expired_attributes"]
  428. else:
  429. self.expired_attributes = set()
  430. self.__dict__.update(
  431. [
  432. (k, state_dict[k])
  433. for k in ("key", "load_options")
  434. if k in state_dict
  435. ]
  436. )
  437. if self.key:
  438. try:
  439. self.identity_token = self.key[2]
  440. except IndexError:
  441. # 1.1 and earlier compat before identity_token
  442. assert len(self.key) == 2
  443. self.key = self.key + (None,)
  444. self.identity_token = None
  445. if "load_path" in state_dict:
  446. self.load_path = PathRegistry.deserialize(state_dict["load_path"])
  447. state_dict["manager"](self, inst, state_dict)
  448. def _reset(self, dict_, key):
  449. """Remove the given attribute and any
  450. callables associated with it."""
  451. old = dict_.pop(key, None)
  452. if old is not None and self.manager[key].impl.collection:
  453. self.manager[key].impl._invalidate_collection(old)
  454. self.expired_attributes.discard(key)
  455. if self.callables:
  456. self.callables.pop(key, None)
  457. def _copy_callables(self, from_):
  458. if "callables" in from_.__dict__:
  459. self.callables = dict(from_.callables)
  460. @classmethod
  461. def _instance_level_callable_processor(cls, manager, fn, key):
  462. impl = manager[key].impl
  463. if impl.collection:
  464. def _set_callable(state, dict_, row):
  465. if "callables" not in state.__dict__:
  466. state.callables = {}
  467. old = dict_.pop(key, None)
  468. if old is not None:
  469. impl._invalidate_collection(old)
  470. state.callables[key] = fn
  471. else:
  472. def _set_callable(state, dict_, row):
  473. if "callables" not in state.__dict__:
  474. state.callables = {}
  475. state.callables[key] = fn
  476. return _set_callable
  477. def _expire(self, dict_, modified_set):
  478. self.expired = True
  479. if self.modified:
  480. modified_set.discard(self)
  481. self.committed_state.clear()
  482. self.modified = False
  483. self._strong_obj = None
  484. if "_pending_mutations" in self.__dict__:
  485. del self.__dict__["_pending_mutations"]
  486. if "parents" in self.__dict__:
  487. del self.__dict__["parents"]
  488. self.expired_attributes.update(
  489. [impl.key for impl in self.manager._loader_impls]
  490. )
  491. if self.callables:
  492. # the per state loader callables we can remove here are
  493. # LoadDeferredColumns, which undefers a column at the instance
  494. # level that is mapped with deferred, and LoadLazyAttribute,
  495. # which lazy loads a relationship at the instance level that
  496. # is mapped with "noload" or perhaps "immediateload".
  497. # Before 1.4, only column-based
  498. # attributes could be considered to be "expired", so here they
  499. # were the only ones "unexpired", which means to make them deferred
  500. # again. For the moment, as of 1.4 we also apply the same
  501. # treatment relationships now, that is, an instance level lazy
  502. # loader is reset in the same way as a column loader.
  503. for k in self.expired_attributes.intersection(self.callables):
  504. del self.callables[k]
  505. for k in self.manager._collection_impl_keys.intersection(dict_):
  506. collection = dict_.pop(k)
  507. collection._sa_adapter.invalidated = True
  508. if self._last_known_values:
  509. self._last_known_values.update(
  510. (k, dict_[k]) for k in self._last_known_values if k in dict_
  511. )
  512. for key in self.manager._all_key_set.intersection(dict_):
  513. del dict_[key]
  514. self.manager.dispatch.expire(self, None)
  515. def _expire_attributes(self, dict_, attribute_names, no_loader=False):
  516. pending = self.__dict__.get("_pending_mutations", None)
  517. callables = self.callables
  518. for key in attribute_names:
  519. impl = self.manager[key].impl
  520. if impl.accepts_scalar_loader:
  521. if no_loader and (impl.callable_ or key in callables):
  522. continue
  523. self.expired_attributes.add(key)
  524. if callables and key in callables:
  525. del callables[key]
  526. old = dict_.pop(key, NO_VALUE)
  527. if impl.collection and old is not NO_VALUE:
  528. impl._invalidate_collection(old)
  529. if (
  530. self._last_known_values
  531. and key in self._last_known_values
  532. and old is not NO_VALUE
  533. ):
  534. self._last_known_values[key] = old
  535. self.committed_state.pop(key, None)
  536. if pending:
  537. pending.pop(key, None)
  538. self.manager.dispatch.expire(self, attribute_names)
  539. def _load_expired(self, state, passive):
  540. """__call__ allows the InstanceState to act as a deferred
  541. callable for loading expired attributes, which is also
  542. serializable (picklable).
  543. """
  544. if not passive & SQL_OK:
  545. return PASSIVE_NO_RESULT
  546. toload = self.expired_attributes.intersection(self.unmodified)
  547. toload = toload.difference(
  548. attr
  549. for attr in toload
  550. if not self.manager[attr].impl.load_on_unexpire
  551. )
  552. self.manager.expired_attribute_loader(self, toload, passive)
  553. # if the loader failed, or this
  554. # instance state didn't have an identity,
  555. # the attributes still might be in the callables
  556. # dict. ensure they are removed.
  557. self.expired_attributes.clear()
  558. return ATTR_WAS_SET
  559. @property
  560. def unmodified(self):
  561. """Return the set of keys which have no uncommitted changes"""
  562. return set(self.manager).difference(self.committed_state)
  563. def unmodified_intersection(self, keys):
  564. """Return self.unmodified.intersection(keys)."""
  565. return (
  566. set(keys)
  567. .intersection(self.manager)
  568. .difference(self.committed_state)
  569. )
  570. @property
  571. def unloaded(self):
  572. """Return the set of keys which do not have a loaded value.
  573. This includes expired attributes and any other attribute that
  574. was never populated or modified.
  575. """
  576. return (
  577. set(self.manager)
  578. .difference(self.committed_state)
  579. .difference(self.dict)
  580. )
  581. @property
  582. def unloaded_expirable(self):
  583. """Return the set of keys which do not have a loaded value.
  584. This includes expired attributes and any other attribute that
  585. was never populated or modified.
  586. """
  587. return self.unloaded
  588. @property
  589. def _unloaded_non_object(self):
  590. return self.unloaded.intersection(
  591. attr
  592. for attr in self.manager
  593. if self.manager[attr].impl.accepts_scalar_loader
  594. )
  595. def _instance_dict(self):
  596. return None
  597. def _modified_event(
  598. self, dict_, attr, previous, collection=False, is_userland=False
  599. ):
  600. if attr:
  601. if not attr.send_modified_events:
  602. return
  603. if is_userland and attr.key not in dict_:
  604. raise sa_exc.InvalidRequestError(
  605. "Can't flag attribute '%s' modified; it's not present in "
  606. "the object state" % attr.key
  607. )
  608. if attr.key not in self.committed_state or is_userland:
  609. if collection:
  610. if previous is NEVER_SET:
  611. if attr.key in dict_:
  612. previous = dict_[attr.key]
  613. if previous not in (None, NO_VALUE, NEVER_SET):
  614. previous = attr.copy(previous)
  615. self.committed_state[attr.key] = previous
  616. if attr.key in self._last_known_values:
  617. self._last_known_values[attr.key] = NO_VALUE
  618. # assert self._strong_obj is None or self.modified
  619. if (self.session_id and self._strong_obj is None) or not self.modified:
  620. self.modified = True
  621. instance_dict = self._instance_dict()
  622. if instance_dict:
  623. has_modified = bool(instance_dict._modified)
  624. instance_dict._modified.add(self)
  625. else:
  626. has_modified = False
  627. # only create _strong_obj link if attached
  628. # to a session
  629. inst = self.obj()
  630. if self.session_id:
  631. self._strong_obj = inst
  632. # if identity map already had modified objects,
  633. # assume autobegin already occurred, else check
  634. # for autobegin
  635. if not has_modified:
  636. # inline of autobegin, to ensure session transaction
  637. # snapshot is established
  638. try:
  639. session = _sessions[self.session_id]
  640. except KeyError:
  641. pass
  642. else:
  643. if session._transaction is None:
  644. session._autobegin()
  645. if inst is None and attr:
  646. raise orm_exc.ObjectDereferencedError(
  647. "Can't emit change event for attribute '%s' - "
  648. "parent object of type %s has been garbage "
  649. "collected."
  650. % (self.manager[attr.key], base.state_class_str(self))
  651. )
  652. def _commit(self, dict_, keys):
  653. """Commit attributes.
  654. This is used by a partial-attribute load operation to mark committed
  655. those attributes which were refreshed from the database.
  656. Attributes marked as "expired" can potentially remain "expired" after
  657. this step if a value was not populated in state.dict.
  658. """
  659. for key in keys:
  660. self.committed_state.pop(key, None)
  661. self.expired = False
  662. self.expired_attributes.difference_update(
  663. set(keys).intersection(dict_)
  664. )
  665. # the per-keys commit removes object-level callables,
  666. # while that of commit_all does not. it's not clear
  667. # if this behavior has a clear rationale, however tests do
  668. # ensure this is what it does.
  669. if self.callables:
  670. for key in (
  671. set(self.callables).intersection(keys).intersection(dict_)
  672. ):
  673. del self.callables[key]
  674. def _commit_all(self, dict_, instance_dict=None):
  675. """commit all attributes unconditionally.
  676. This is used after a flush() or a full load/refresh
  677. to remove all pending state from the instance.
  678. - all attributes are marked as "committed"
  679. - the "strong dirty reference" is removed
  680. - the "modified" flag is set to False
  681. - any "expired" markers for scalar attributes loaded are removed.
  682. - lazy load callables for objects / collections *stay*
  683. Attributes marked as "expired" can potentially remain
  684. "expired" after this step if a value was not populated in state.dict.
  685. """
  686. self._commit_all_states([(self, dict_)], instance_dict)
  687. @classmethod
  688. def _commit_all_states(self, iter_, instance_dict=None):
  689. """Mass / highly inlined version of commit_all()."""
  690. for state, dict_ in iter_:
  691. state_dict = state.__dict__
  692. state.committed_state.clear()
  693. if "_pending_mutations" in state_dict:
  694. del state_dict["_pending_mutations"]
  695. state.expired_attributes.difference_update(dict_)
  696. if instance_dict and state.modified:
  697. instance_dict._modified.discard(state)
  698. state.modified = state.expired = False
  699. state._strong_obj = None
  700. class AttributeState(object):
  701. """Provide an inspection interface corresponding
  702. to a particular attribute on a particular mapped object.
  703. The :class:`.AttributeState` object is accessed
  704. via the :attr:`.InstanceState.attrs` collection
  705. of a particular :class:`.InstanceState`::
  706. from sqlalchemy import inspect
  707. insp = inspect(some_mapped_object)
  708. attr_state = insp.attrs.some_attribute
  709. """
  710. def __init__(self, state, key):
  711. self.state = state
  712. self.key = key
  713. @property
  714. def loaded_value(self):
  715. """The current value of this attribute as loaded from the database.
  716. If the value has not been loaded, or is otherwise not present
  717. in the object's dictionary, returns NO_VALUE.
  718. """
  719. return self.state.dict.get(self.key, NO_VALUE)
  720. @property
  721. def value(self):
  722. """Return the value of this attribute.
  723. This operation is equivalent to accessing the object's
  724. attribute directly or via ``getattr()``, and will fire
  725. off any pending loader callables if needed.
  726. """
  727. return self.state.manager[self.key].__get__(
  728. self.state.obj(), self.state.class_
  729. )
  730. @property
  731. def history(self):
  732. """Return the current **pre-flush** change history for
  733. this attribute, via the :class:`.History` interface.
  734. This method will **not** emit loader callables if the value of the
  735. attribute is unloaded.
  736. .. note::
  737. The attribute history system tracks changes on a **per flush
  738. basis**. Each time the :class:`.Session` is flushed, the history
  739. of each attribute is reset to empty. The :class:`.Session` by
  740. default autoflushes each time a :class:`_query.Query` is invoked.
  741. For
  742. options on how to control this, see :ref:`session_flushing`.
  743. .. seealso::
  744. :meth:`.AttributeState.load_history` - retrieve history
  745. using loader callables if the value is not locally present.
  746. :func:`.attributes.get_history` - underlying function
  747. """
  748. return self.state.get_history(self.key, PASSIVE_NO_INITIALIZE)
  749. def load_history(self):
  750. """Return the current **pre-flush** change history for
  751. this attribute, via the :class:`.History` interface.
  752. This method **will** emit loader callables if the value of the
  753. attribute is unloaded.
  754. .. note::
  755. The attribute history system tracks changes on a **per flush
  756. basis**. Each time the :class:`.Session` is flushed, the history
  757. of each attribute is reset to empty. The :class:`.Session` by
  758. default autoflushes each time a :class:`_query.Query` is invoked.
  759. For
  760. options on how to control this, see :ref:`session_flushing`.
  761. .. seealso::
  762. :attr:`.AttributeState.history`
  763. :func:`.attributes.get_history` - underlying function
  764. .. versionadded:: 0.9.0
  765. """
  766. return self.state.get_history(self.key, PASSIVE_OFF ^ INIT_OK)
  767. class PendingCollection(object):
  768. """A writable placeholder for an unloaded collection.
  769. Stores items appended to and removed from a collection that has not yet
  770. been loaded. When the collection is loaded, the changes stored in
  771. PendingCollection are applied to it to produce the final result.
  772. """
  773. def __init__(self):
  774. self.deleted_items = util.IdentitySet()
  775. self.added_items = util.OrderedIdentitySet()
  776. def append(self, value):
  777. if value in self.deleted_items:
  778. self.deleted_items.remove(value)
  779. else:
  780. self.added_items.add(value)
  781. def remove(self, value):
  782. if value in self.added_items:
  783. self.added_items.remove(value)
  784. else:
  785. self.deleted_items.add(value)