MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _load_scalar_attributes

Function _load_scalar_attributes

lib/sqlalchemy/orm/loading.py:1608–1693  ·  view source on GitHub ↗

initiate a column-based attribute refresh operation.

(mapper, state, attribute_names, passive)

Source from the content-addressed store, hash-verified

1606
1607
1608def _load_scalar_attributes(mapper, state, attribute_names, passive):
1609 """initiate a column-based attribute refresh operation."""
1610
1611 # assert mapper is _state_mapper(state)
1612 session = state.session
1613 if not session:
1614 raise orm_exc.DetachedInstanceError(
1615 "Instance %s is not bound to a Session; "
1616 "attribute refresh operation cannot proceed" % (state_str(state))
1617 )
1618
1619 no_autoflush = bool(passive & attributes.NO_AUTOFLUSH)
1620
1621 # in the case of inheritance, particularly concrete and abstract
1622 # concrete inheritance, the class manager might have some keys
1623 # of attributes on the superclass that we didn't actually map.
1624 # These could be mapped as "concrete, don't load" or could be completely
1625 # excluded from the mapping and we know nothing about them. Filter them
1626 # here to prevent them from coming through.
1627 if attribute_names:
1628 attribute_names = attribute_names.intersection(mapper.attrs.keys())
1629
1630 if mapper.inherits and not mapper.concrete:
1631 # load based on committed attributes in the object, formed into
1632 # a truncated SELECT that only includes relevant tables. does not
1633 # currently use state.key
1634 statement = mapper._optimized_get_statement(state, attribute_names)
1635 if statement is not None:
1636 # undefer() isn't needed here because statement has the
1637 # columns needed already, this implicitly undefers that column
1638 stmt = FromStatement(mapper, statement)
1639
1640 return _load_on_ident(
1641 session,
1642 stmt,
1643 None,
1644 only_load_props=attribute_names,
1645 refresh_state=state,
1646 no_autoflush=no_autoflush,
1647 )
1648
1649 # normal load, use state.key as the identity to SELECT
1650 has_key = bool(state.key)
1651
1652 if has_key:
1653 identity_key = state.key
1654 else:
1655 # this codepath is rare - only valid when inside a flush, and the
1656 # object is becoming persistent but hasn't yet been assigned
1657 # an identity_key.
1658 # check here to ensure we have the attrs we need.
1659 pk_attrs = [
1660 mapper._columntoproperty[col].key for col in mapper.primary_key
1661 ]
1662 if state.expired_attributes.intersection(pk_attrs):
1663 raise sa_exc.InvalidRequestError(
1664 "Instance %s cannot be refreshed - it's not "
1665 " persistent and does not "

Callers

nothing calls this directly

Calls 10

selectFunction · 0.90
state_strFunction · 0.85
FromStatementClass · 0.85
_load_on_identFunction · 0.85
intersectionMethod · 0.45
keysMethod · 0.45
issubsetMethod · 0.45
issupersetMethod · 0.45

Tested by

no test coverage detected