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

Method _flush

lib/sqlalchemy/orm/session.py:4372–4489  ·  view source on GitHub ↗
(self, objects: Optional[Sequence[object]] = None)

Source from the content-addressed store, hash-verified

4370 )
4371
4372 def _flush(self, objects: Optional[Sequence[object]] = None) -> None:
4373 dirty = self._dirty_states
4374 if not dirty and not self._deleted and not self._new:
4375 self.identity_map._modified.clear()
4376 return
4377
4378 flush_context = UOWTransaction(self)
4379
4380 if self.dispatch.before_flush:
4381 self.dispatch.before_flush(self, flush_context, objects)
4382 # re-establish "dirty states" in case the listeners
4383 # added
4384 dirty = self._dirty_states
4385
4386 deleted = set(self._deleted)
4387 new = set(self._new)
4388
4389 dirty = set(dirty).difference(deleted)
4390
4391 # create the set of all objects we want to operate upon
4392 if objects:
4393 # specific list passed in
4394 objset = set()
4395 for o in objects:
4396 try:
4397 state = attributes.instance_state(o)
4398
4399 except exc.NO_STATE as err:
4400 raise exc.UnmappedInstanceError(o) from err
4401 objset.add(state)
4402 else:
4403 objset = None
4404
4405 # store objects whose fate has been decided
4406 processed = set()
4407
4408 # put all saves/updates into the flush context. detect top-level
4409 # orphans and throw them into deleted.
4410 if objset:
4411 proc = new.union(dirty).intersection(objset).difference(deleted)
4412 else:
4413 proc = new.union(dirty).difference(deleted)
4414
4415 for state in proc:
4416 is_orphan = _state_mapper(state)._is_orphan(state)
4417
4418 is_persistent_orphan = is_orphan and state.has_identity
4419
4420 if (
4421 is_orphan
4422 and not is_persistent_orphan
4423 and state._orphaned_outside_of_session
4424 ):
4425 self._expunge_states([state])
4426 else:
4427 _reg = flush_context.register_object(
4428 state, isdelete=is_persistent_orphan
4429 )

Callers 1

flushMethod · 0.95

Calls 15

_expunge_statesMethod · 0.95
register_objectMethod · 0.95
_autobegin_tMethod · 0.95
executeMethod · 0.95
UOWTransactionClass · 0.85
_state_mapperFunction · 0.85
_is_orphanMethod · 0.80
_beginMethod · 0.80
after_flushMethod · 0.80
_commit_all_statesMethod · 0.80
after_flush_postexecMethod · 0.80

Tested by

no test coverage detected