summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm/state.py
Commit message (Collapse)AuthorAgeFilesLines
* Remove old versionadded and versionchangedFederico Caselli2023-04-121-11/+0
| | | | | | | Removed versionadded and versionchanged for version prior to 1.2 since they are no longer useful. Change-Id: I5c53d1188bc5fec3ab4be39ef761650ed8fa6d3e
* happy new year 2023Mike Bayer2023-01-031-1/+1
| | | | Change-Id: I625af65b3fb1815b1af17dc2ef47dd697fdc3fb1
* rework ORM mapping docsMike Bayer2022-06-211-1/+3
| | | | | | | | | | prepare docs for newly incoming mapper styles, including new dataclass mapping. move the existing dataclass/attrs docs all into their own section and try to improve organization and wording into the relatively recent "mapping styles" document. Change-Id: I0b5e2a5b6a70db65ab19b5bb0a2bb7df20e0b498
* revenge of pep 484Mike Bayer2022-05-151-1/+17
| | | | | | trying to get remaining must-haves for ORM Change-Id: I66a3ecbbb8e5ba37c818c8a92737b576ecf012f7
* update for flake8-future-imports 0.0.5Mike Bayer2022-05-141-1/+0
| | | | | | | | a whole bunch of errors were apparently blocked by 0.0.4 being installed. Fixes: #8020 Change-Id: I22a0faeaabe03de501897893391946d677c2df7e
* pep484: attributes and relatedMike Bayer2022-05-031-3/+6
| | | | | | | also implements __slots__ for QueryableAttribute, InstrumentedAttribute, Relationship.Comparator. Change-Id: I47e823160706fc35a616f1179a06c7864089e5b5
* pep484 ORM / SQL result supportMike Bayer2022-04-271-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | after some experimentation it seems mypy is more amenable to the generic types being fully integrated rather than having separate spin-off types. so key structures like Result, Row, Select become generic. For DML Insert, Update, Delete, these are spun into type-specific subclasses ReturningInsert, ReturningUpdate, ReturningDelete, which is fine since the "row-ness" of these constructs doesn't happen until returning() is called in any case. a Tuple based model is then integrated so that these objects can carry along information about their return types. Overloads at the .execute() level carry through the Tuple from the invoked object to the result. To suit the issue of AliasedClass generating attributes that are dynamic, experimented with a custom subclass AsAliased, but then just settled on having aliased() lie to the type checker and return `Type[_O]`, essentially. will need some type-related accessors for with_polymorphic() also. Additionally, identified an issue in Update when used "mysql style" against a join(), it basically doesn't work if asked to UPDATE two tables on the same column name. added an error message to the specific condition where it happens with a very non-specific error message that we hit a thing we can't do right now, suggest multi-table update as a possible cause. Change-Id: I5eff7eefe1d6166ee74160b2785c5e6a81fa8b95
* pep-484: asyncioMike Bayer2022-04-111-19/+17
| | | | | | | | | | | | | | | | | | | | | in this patch the asyncio/events.py module, which existed only to raise errors when trying to attach event listeners, is removed, as we were already coding an asyncio-specific workaround in upstream Pool / Session to raise this error, just moved the error out to the target and did the same thing for Engine. We also add an async_sessionmaker class. The initial rationale here is because sessionmaker() is hardcoded to Session subclasses, and there's not a way to get the use case of sessionmaker(class_=AsyncSession) to type correctly without changing the sessionmaker() symbol itself to be a function and not a class, which gets too complicated for what this is. Additionally, _SessionClassMethods has only three methods on it, one of which is not usable with asyncio (close_all()), the others not generally used from the session class. Change-Id: I064a5fa5d91cc8d5bbe9597437536e37b4e801fe
* pep-484: session, instancestate, etcMike Bayer2022-04-121-143/+241
| | | | | | | | Also adds some fixes to annotation-based mapping that have come up, as well as starts to add more pep-484 test cases Change-Id: Ia722bbbc7967a11b23b66c8084eb61df9d233fee
* pep-484: the pep-484ening, SQL part threeMike Bayer2022-03-301-1/+1
| | | | | | | | | | | | | | | hitting DML which is causing us to open up the ColumnCollection structure a bit, as we do put anonymous column expressions with None here. However, we still want Table /TableClause to have named column collections that don't return None, so parametrize the "key" in this collection also. * rename some "immutable" elements to "readonly". we change the contents of immutablecolumncollection underneath, so it's not "immutable" Change-Id: I2593995a4e5c6eae874bed5bf76117198be8ae97
* pep-484 for sqlalchemy.event; use future annotationsMike Bayer2022-02-151-0/+2
| | | | | | | | | | __future__.annotations mode allows us to use non-string annotations for argument and return types in most cases, but more importantly it removes a large amount of runtime overhead that would be spent in evaluating the annotations. Change-Id: I2f5b6126fe0019713fc50001be3627b664019ede References: #6810
* change state.load_options to a tupleMike Bayer2022-01-111-1/+1
| | | | | | | | | | | | | | | | having this be an immutable sequence is safer and possibly lower overhead. The change here went in with no issues save for tests that asserted it was a set. InstanceState.load_options is only referred towards when the object is first loaded, and then within the logic that emits an object refresh as well as within a lazy loader. it's only accessed as a whole collection. Fixes: #7558 Change-Id: Id1adbec0f93bcfbfc934ec9cd39e71e74727845d
* happy new year 2022Mike Bayer2022-01-061-1/+1
| | | | Change-Id: I49abf2607e0eb0623650efdf0091b1fb3db737ea
* Remove object in class definitionFederico Caselli2021-11-221-2/+2
| | | | | References: #4600 Change-Id: I2a62ddfe00bc562720f0eae700a497495d7a987a
* Replace all http:// links to https://Federico Caselli2021-07-041-1/+1
| | | | | | Also replace http://pypi.python.org/pypi with https://pypi.org/project Change-Id: I84b5005c39969a82140706472989f2a30b0c7685
* Implement proxy back reference system for asyncioMike Bayer2021-06-021-0/+35
| | | | | | | | | | | | | | | | | | | | | | | | | | Implemented a new registry architecture that allows the ``Async`` version of an object, like ``AsyncSession``, ``AsyncConnection``, etc., to be locatable given the proxied "sync" object, i.e. ``Session``, ``Connection``. Previously, to the degree such lookup functions were used, an ``Async`` object would be re-created each time, which was less than ideal as the identity and state of the "async" object would not be preserved across calls. From there, new helper functions :func:`_asyncio.async_object_session`, :func:`_asyncio.async_session` as well as a new :class:`_orm.InstanceState` attribute :attr:`_orm.InstanceState.asyncio_session` have been added, which are used to retrieve the original :class:`_asyncio.AsyncSession` associated with an ORM mapped object, a :class:`_orm.Session` associated with an :class:`_asyncio.AsyncSession`, and an :class:`_asyncio.AsyncSession` associated with an :class:`_orm.InstanceState`, respectively. This patch also implements new methods :meth:`_asyncio.AsyncSession.in_nested_transaction`, :meth:`_asyncio.AsyncSession.get_transaction`, :meth:`_asyncio.AsyncSession.get_nested_transaction`. Fixes: #6319 Change-Id: Ia452a7e7ce9bad3ff8846c7dea8d45c839ac9fac
* Ensure autobegin occurs for attribute changes; Document autobeginMike Bayer2021-04-261-2/+27
| | | | | | | | | | | | | | | | | | | | | | | The Session autobegin feature was not anticipated as having any behavioral changes other than the event hook being called at a different time, however as autobegin impacts the behavior of the commit() and rollback() methods in that they can now be no-ops in non-autocommit mode, document the behavior fully. Fixed issue where the new :ref:`session_autobegin` behavior failed to "autobegin" in the case where an existing persistent object has an attribute change, which would then impact the behavior of :meth:`_orm.Session.rollback` in that no snapshot was created to be rolled back. The "attribute modify" mechanics have been updated to ensure "autobegin", which does not perform any database work, does occur when persistent attributes change in the same manner as when :meth:`_orm.Session.add` is called. This is a regression as in 1.3, the rollback() method always had a transaction to roll back and would expire every time. Fixes: #6360 Fixes: #6359 Change-Id: I69f231a206f49e3231275d23bbe2cafd4e2bf3ba
* happy new yearMike Bayer2021-01-041-1/+1
| | | | Change-Id: Ic5bb19ca8be3cb47c95a0d3315d84cb484bac47c
* upgrade to black 20.8b1Mike Bayer2020-09-281-2/+2
| | | | | | | It's better, the majority of these changes look more readable to me. also found some docstrings that had formatting / quoting issues. Change-Id: I582a45fde3a5648b2f36bab96bad56881321899b
* Fix a wide variety of typos and broken linksaplatkouski2020-06-251-6/+6
| | | | | | | | | | | | Note the PR has a few remaining doc linking issues listed in the comment that must be addressed separately. Signed-off-by: aplatkouski <5857672+aplatkouski@users.noreply.github.com> Closes: #5371 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/5371 Pull-request-sha: 7e7d233cf3a0c66980c27db0fcdb3c7d93bc2510 Change-Id: I9c36e8d8804483950db4b42c38ee456e384c59e3
* Run search and replace of symbolic module namesMike Bayer2020-04-141-5/+7
| | | | | | | | Replaces a wide array of Sphinx-relative doc references with an abbreviated absolute form now supported by zzzeeksphinx. Change-Id: I94bffcc3f37885ffdde6238767224296339698a2
* Run autoflush for column attribute load operationsMike Bayer2020-04-031-2/+1
| | | | | | | | | | | | | | | | | The "autoflush" behavior of :class:`.Query` will now trigger for nearly all ORM level attribute load operations, including when a deferred column is loaded as well as when an expired column is loaded. Previously, autoflush on load of expired or unloaded attributes was limited to relationship-bound attributes only. However, this led to the issue where column-based attributes that also depended on other rows, or even other columns in the same row, in order to express the correct value, would show an effectively stale value when accessed as there could be pending changes in the session left to be flushed. Autoflush is now disabled only in some cases where attributes are being unexpired in the context of a history operation. Fixes: #5226 Change-Id: Ibd965b30918cd273ae020411a704bf2bb1891f59
* Simplified module pre-loading strategy and made it linter friendlyFederico Caselli2020-03-071-6/+6
| | | | | | | | | | | | | | | | | Introduced a modules registry to register modules that should be lazily loaded in the package init. This ensures that they are in the system module cache, avoiding potential thread safety issues as when importing them directly in the function that uses them. The module registry is used to obtain these modules directly, ensuring that the all the lazily loaded modules are resolved at the proper time This replaces dependency_for decorator and the dependencies decorator logic, removing the need to pass the resolved modules as arguments of the decodated functions and removes possible errors caused by linters. Fixes: #4689 Fixes: #4656 Change-Id: I2e291eba4297867fc0ddb5d875b9f7af34751d01
* Warn for runid changing in load events; add restore_load_context flagMike Bayer2020-01-311-12/+4
| | | | | | | | | | | | | | | | | | | | Added a new flag :paramref:`.InstanceEvents.restore_load_context` and :paramref:`.SessionEvents.restore_load_context` which apply to the :meth:`.InstanceEvents.load`, :meth:`.InstanceEvents.refresh`, and :meth:`.SessionEvents.loaded_as_persistent` events, which when set will restore the "load context" of the object after the event hook has been called. This ensures that the object remains within the "loader context" of the load operation that is already ongoing, rather than the object being transferred to a new load context due to refresh operations which may have occurred in the event. A warning is now emitted when this condition occurs, which recommends use of the flag to resolve this case. The flag is "opt-in" so that there is no risk introduced to existing applications. The change additionally adds support for the ``raw=True`` flag to session lifecycle events. Fixes: #5129 Change-Id: I2912f48ac8c5636297d63ed383454930e8e9a6a3
* InstanceState default path is RootRegistry, not tupleMike Bayer2020-01-221-1/+1
| | | | | | | | | | | | | | | | Fixed regression caused in 1.3.13 by :ticket:`5056` where a refactor of the ORM path registry system made it such that a path could no longer be compared to an empty tuple, which can occur in a particular kind of joined eager loading path. The "empty tuple" use case has been resolved so that the path registry is compared to a path registry in all cases; the :class:`.PathRegistry` object itself now implements ``__eq__()`` and ``__ne__()`` methods which will take place for all equality comparisons and continue to succeed in the not anticipated case that a non- :class:`.PathRegistry` object is compared, while emitting a warning that this object should not be the subject of the comparison. Fixes: #5110 Change-Id: I6cab6cd771c131d12b17939b369212f12c6bee16
* happy new yearMike Bayer2020-01-011-1/+1
| | | | Change-Id: I08440dc25e40ea1ccea1778f6ee9e28a00808235
* Implement raiseload for deferred columnsMike Bayer2019-10-161-11/+8
| | | | | | | | | | | | | | | | | | | | | Added "raiseload" feature for ORM mapped columns. As part of this change, the behavior of "deferred" is now more strict; an attribute that is set up as "deferred" at the mapper level no longer participates in an "unexpire" operation; that is, when an unexpire loads all the expired columns of an object which are not themselves in a deferred group, those which are mapper-level deferred will never be loaded. Deferral options set at query time should always be reset by an expiration operation. Renames deferred_scalar_loader to expired_attribute_loader Unfortunately we can't have raiseload() do this because it would break existing wildcard behavior. Fixes: #4826 Change-Id: I30d9a30236e0b69134e4094fb7c1ad2267f089d1
* Run eager loaders on unexpireMike Bayer2019-08-231-1/+12
| | | | | | | | | | | | Eager loaders, such as joined loading, SELECT IN loading, etc., when configured on a mapper or via query options will now be invoked during the refresh on an expired object; in the case of selectinload and subqueryload, since the additional load is for a single object only, the "immediateload" scheme is used in these cases which resembles the single-parent query emitted by lazy loading. Change-Id: I7ca2c77bff58dc21015d60093a88c387937376b2 Fixes: #1763
* Hold implicitly created collections in a pending areaMike Bayer2019-05-271-0/+4
| | | | | | | | | | | | | | | Accessing a collection-oriented attribute on a newly created object no longer mutates ``__dict__``, but still returns an empty collection as has always been the case. This allows collection-oriented attributes to work consistently in comparison to scalar attributes which return ``None``, but also don't mutate ``__dict__``. In order to accommodate for the collection being mutated, the same empty collection is returned each time once initially created, and when it is mutated (e.g. an item appended, added, etc.) it is then moved into ``__dict__``. This removes the last of mutating side-effects on read-only attribute access within the ORM. Fixes: #4519 Change-Id: I06a058d24e6eb24b5c6b6092d3f8b31cf9c244ae
* Note for history methods that history is reset per-flush.Mike Bayer2019-02-091-2/+19
| | | | Change-Id: I9bc4d0ddfa93f13e6717b89fa9934f1b8052147f
* Fix many spell glitchesLele Gaifax2019-01-251-1/+1
| | | | | | | | | | | | This affects mostly docstrings, except in orm/events.py::dispose_collection() where one parameter gets renamed: given that the method is empty, it seemed reasonable to me to fix that too. Closes: #4440 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/4440 Pull-request-sha: 779ed75acb6142e1f1daac467b5b14134529bb4b Change-Id: Ic0553fe97853054b09c2453af76d96363de6eb0e
* happy new yearMike Bayer2019-01-111-1/+1
| | | | Change-Id: I6a71f4924d046cf306961c58dffccf21e9c03911
* Post black reformattingMike Bayer2019-01-061-19/+20
| | | | | | | | | | | | | Applied on top of a pure run of black -l 79 in I7eda77fed3d8e73df84b3651fd6cfcfe858d4dc9, this set of changes resolves all remaining flake8 conditions for those codes we have enabled in setup.cfg. Included are resolutions for all remaining flake8 issues including shadowed builtins, long lines, import order, unused imports, duplicate imports, and docstring issues. Change-Id: I4f72d3ba1380dd601610ff80b8fb06a2aff8b0fe
* Run black -l 79 against all source filesMike Bayer2019-01-061-104/+129
| | | | | | | | | | | | | | This is a straight reformat run using black as is, with no edits applied at all. The black run will format code consistently, however in some cases that are prevalent in SQLAlchemy code it produces too-long lines. The too-long lines will be resolved in the following commit that will resolve all remaining flake8 issues including shadowed builtins, long lines, import order, unused imports, duplicate imports, and docstring issues. Change-Id: I7eda77fed3d8e73df84b3651fd6cfcfe858d4dc9
* Modernize deferred callable for many-to-one comparisonMike Bayer2018-11-101-2/+28
| | | | | | | | | | | | | | | | | | | | | | Improved the behavior of a relationship-bound many-to-one object expression such that the retrieval of column values on the related object are now resilient against the object being detached from its parent :class:`.Session`, even if the attribute has been expired. New features within the :class:`.InstanceState` are used to memoize the last known value of a particular column attribute before its expired, so that the expression can still evaluate when the object is detached and expired at the same time. Error conditions are also improved using modern attribute state features to produce more specific messages as needed. To support the value being mutated while also being resilient towards expiration, a new feature to InstanceState is added ._last_known_values which holds onto the expired value when an individual key is expired. Only takes effect specific to keys and InstanceState objects that received a special instruction so this does not add to overall memory/latency. Fixes: #4359 Change-Id: Iff272e667bf741074549db550bf65348553ca8e7
* Add .info to InstanceStateMike Bayer2018-10-011-2/+4
| | | | | | | | Added ``.info`` dictionary to the :class:`.InstanceState` class, the object that comes from calling :func:`.inspect` on a mapped object. Fixes: #4257 Change-Id: I32d043f369edb708a17eec2e0b8876db0c1891b4
* Look for dict builtin in InstanceState cleanupRomuald Brunet2018-09-181-0/+7
| | | | | | | | | | Added a check within the weakref cleanup for the :class:`.InstanceState` object to check for the presence of the ``dict`` builtin, in an effort to reduce error messages generated when these cleanups occur during interpreter shutdown. Pull request courtesy Romuald Brunet. Change-Id: If27b94d50a32767de8b4147c09fa423f71596004 Pull-request: https://github.com/zzzeek/sqlalchemy/pull/472
* happy new yearMike Bayer2018-01-121-1/+1
| | | | Change-Id: I3ef36bfd0cb0ba62b3123c8cf92370a43156cf8f
* Add an identity_token to the identity keyMike Bayer2017-12-221-10/+24
| | | | | | | | | For the purposes of assisting with sharded setups, add a new member to the identity key that can be customized. this allows sharding across databases where the primary key space is shared. Change-Id: Iae3909f5d4c501b62c10d0371fbceb01abda51db Fixes: #4137
* Don't expire "deferred" attributes in make_transient_to_detachedMike Bayer2017-09-261-0/+13
| | | | | | | | | | Fixed issue where the :func:`.make_transient_to_detached` function would expire all attributes on the target object, including "deferred" attributes, which has the effect of the attribute being undeferred for the next refesh, causing an unexpected load of the attribute. Change-Id: I82a385e3033e3f3c31569b1e908efb5f258d0f27 Fixes: #4084
* Enable multi-level selectin polymorphic loadingMike Bayer2017-08-071-0/+1
| | | | | Change-Id: Icc742bbeecdb7448ce84caccd63e086af16e81c1 Fixes: #4026
* Raise on flag_modified() for non-present attributeMike Bayer2017-03-211-14/+19
| | | | | | | | | | | | The :func:`.attributes.flag_modified` function now raises :class:`.InvalidRequestError` if the named attribute key is not present within the object, as this is assumed to be present in the flush process. To mark an object "dirty" for a flush without referring to any specific attribute, the :func:`.attributes.flag_dirty` function may be used. Change-Id: I6c64e4d253c239e38632f38c27bb16e68fe8dfbe Fixes: #3753
* update for 2017 copyrightMike Bayer2017-01-041-1/+1
| | | | Change-Id: I4e8c2aa8fe817bb2af8707410fa0201f938781de
* Ensure strong ref to obj before calling persistent_to_deleted, othersMike Bayer2016-10-031-4/+12
| | | | | | | | | | | | Add checks in spots where state.obj() might be late-GC'ed before we get a chance to call the event. There may be more cases of these which we should address as they come up. The Session should always be maintaining strong refs to objects that have pending operations left on them, so for these cases we need to ensure that ref remains long enough for the event to be called. Change-Id: I1a7c7bc57130acc11f54ad55924af2e36ac75101 Fixes: #3808
* - don't load deferred columns on unexpire for merge with load=False,Diana Clarke2016-04-061-1/+11
| | | | | | | fixes #3488 Change-Id: Ic9577b800e4a4e2465ec7f3a2e95bd231f5337ee Co-Authored-By: Mike Bayer <mike_mp@zzzcomputing.com>
* - fully hyperlink the docstring for make_transientMike Bayer2016-02-081-1/+1
| | | | | | | | | - establish make_transient and make_transient_to_detached as special-use, advanced use only functions - list all conditions under make_transient() under which an attribute will not be loaded and establish that make_transient() does not attempt to load all attributes before detaching the object from its session, fixes #3640
* - happy new yearMike Bayer2016-01-291-1/+1
|
* - reduce some callcounts within the new eventsMike Bayer2015-09-031-1/+1
|
* - The :class:`.SessionEvents` suite now includes events to allowticket_2677Mike Bayer2015-09-021-7/+105
| | | | | | | | | | | | | | | | | | | | | unambiguous tracking of all object lifecycle state transitions in terms of the :class:`.Session` itself, e.g. pending, transient, persistent, detached. The state of the object within each event is also defined. fixes #2677 - Added a new session lifecycle state :term:`deleted`. This new state represents an object that has been deleted from the :term:`persistent` state and will move to the :term:`detached` state once the transaction is committed. This resolves the long-standing issue that objects which were deleted existed in a gray area between persistent and detached. The :attr:`.InstanceState.persistent` accessor will **no longer** report on a deleted object as persistent; the :attr:`.InstanceState.deleted` accessor will instead be True for these objects, until they become detached. - The :paramref:`.Session.weak_identity_map` parameter is deprecated. See the new recipe at :ref:`session_referencing_behavior` for an event-based approach to maintaining strong identity map behavior. references #3517
* - repair the inspection hook in sqltypes to not be fooledMike Bayer2015-08-221-0/+2
| | | | by mock and other __getattr__ impostors