MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / enable_relationship_loading

Method enable_relationship_loading

lib/sqlalchemy/orm/session.py:4361–4424  ·  view source on GitHub ↗

Associate an object with this :class:`.Session` for related object loading. .. warning:: :meth:`.enable_relationship_loading` exists to serve special use cases and is not recommended for general use. Accesses of attributes mapped with :func:`_orm.re

(self, obj: object)

Source from the content-addressed store, hash-verified

4359 self._update_impl(state)
4360
4361 def enable_relationship_loading(self, obj: object) -> None:
4362 """Associate an object with this :class:`.Session` for related
4363 object loading.
4364
4365 .. warning::
4366
4367 :meth:`.enable_relationship_loading` exists to serve special
4368 use cases and is not recommended for general use.
4369
4370 Accesses of attributes mapped with :func:`_orm.relationship`
4371 will attempt to load a value from the database using this
4372 :class:`.Session` as the source of connectivity. The values
4373 will be loaded based on foreign key and primary key values
4374 present on this object - if not present, then those relationships
4375 will be unavailable.
4376
4377 The object will be attached to this session, but will
4378 **not** participate in any persistence operations; its state
4379 for almost all purposes will remain either "transient" or
4380 "detached", except for the case of relationship loading.
4381
4382 Also note that backrefs will often not work as expected.
4383 Altering a relationship-bound attribute on the target object
4384 may not fire off a backref event, if the effective value
4385 is what was already loaded from a foreign-key-holding value.
4386
4387 The :meth:`.Session.enable_relationship_loading` method is
4388 similar to the ``load_on_pending`` flag on :func:`_orm.relationship`.
4389 Unlike that flag, :meth:`.Session.enable_relationship_loading` allows
4390 an object to remain transient while still being able to load
4391 related items.
4392
4393 To make a transient object associated with a :class:`.Session`
4394 via :meth:`.Session.enable_relationship_loading` pending, add
4395 it to the :class:`.Session` using :meth:`.Session.add` normally.
4396 If the object instead represents an existing identity in the database,
4397 it should be merged using :meth:`.Session.merge`.
4398
4399 :meth:`.Session.enable_relationship_loading` does not improve
4400 behavior when the ORM is used normally - object references should be
4401 constructed at the object level, not at the foreign key level, so
4402 that they are present in an ordinary way before flush()
4403 proceeds. This method is not intended for general use.
4404
4405 .. seealso::
4406
4407 :paramref:`_orm.relationship.load_on_pending` - this flag
4408 allows per-relationship loading of many-to-ones on items that
4409 are pending.
4410
4411 :func:`.make_transient_to_detached` - allows for an object to
4412 be added to a :class:`.Session` without SQL emitted, which then
4413 will unexpire attributes on access.
4414
4415 """
4416 try:
4417 state = attributes.instance_state(obj)
4418 except exc.NO_STATE as err:

Calls 2

_before_attachMethod · 0.95
_after_attachMethod · 0.95