MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / load_scalar_attributes

Function load_scalar_attributes

lib/sqlalchemy/orm/loading.py:1601–1686  ·  view source on GitHub ↗

initiate a column-based attribute refresh operation.

(mapper, state, attribute_names, passive)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 11

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
set_label_styleMethod · 0.45

Tested by

no test coverage detected