MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _organize_states_for_save

Function _organize_states_for_save

lib/sqlalchemy/orm/persistence.py:210–287  ·  view source on GitHub ↗

Make an initial pass across a set of states for INSERT or UPDATE. This includes splitting out into distinct lists for each, calling before_insert/before_update, obtaining key information for each state including its dictionary, mapper, the connection to use for the execution per

(base_mapper, states, uowtransaction)

Source from the content-addressed store, hash-verified

208
209
210def _organize_states_for_save(base_mapper, states, uowtransaction):
211 """Make an initial pass across a set of states for INSERT or
212 UPDATE.
213
214 This includes splitting out into distinct lists for
215 each, calling before_insert/before_update, obtaining
216 key information for each state including its dictionary,
217 mapper, the connection to use for the execution per state,
218 and the identity flag.
219
220 """
221
222 for state, dict_, mapper, connection in _connections_for_states(
223 base_mapper, uowtransaction, states
224 ):
225 has_identity = bool(state.key)
226
227 instance_key = state.key or mapper._identity_key_from_state(state)
228
229 row_switch = update_version_id = None
230
231 # call before_XXX extensions
232 if not has_identity:
233 mapper.dispatch.before_insert(mapper, connection, state)
234 else:
235 mapper.dispatch.before_update(mapper, connection, state)
236
237 if mapper._validate_polymorphic_identity:
238 mapper._validate_polymorphic_identity(mapper, state, dict_)
239
240 # detect if we have a "pending" instance (i.e. has
241 # no instance_key attached to it), and another instance
242 # with the same identity key already exists as persistent.
243 # convert to an UPDATE if so.
244 if (
245 not has_identity
246 and instance_key in uowtransaction.session.identity_map
247 ):
248 instance = uowtransaction.session.identity_map[instance_key]
249 existing = attributes.instance_state(instance)
250
251 if not uowtransaction.was_already_deleted(existing):
252 if not uowtransaction.is_deleted(existing):
253 util.warn(
254 "New instance %s with identity key %s conflicts "
255 "with persistent instance %s"
256 % (state_str(state), instance_key, state_str(existing))
257 )
258 else:
259 base_mapper._log_debug(
260 "detected row switch for identity %s. "
261 "will update %s, remove %s from "
262 "transaction",
263 instance_key,
264 state_str(state),
265 state_str(existing),
266 )
267

Callers 1

_save_objFunction · 0.85

Calls 12

_connections_for_statesFunction · 0.85
state_strFunction · 0.85
was_already_deletedMethod · 0.80
is_deletedMethod · 0.80
remove_state_actionsMethod · 0.80
before_insertMethod · 0.45
before_updateMethod · 0.45
warnMethod · 0.45
_log_debugMethod · 0.45

Tested by

no test coverage detected