Skip to content

Write a delete without a state when its row is already gone - #182

Merged
WiktorB2004 merged 2 commits into
mainfrom
record-state-gone-row
Dec 4, 2026
Merged

WiktorB2004 merged 2 commits into
mainfrom
record-state-gone-row

Conversation

@WiktorB2004

@WiktorB2004 WiktorB2004 commented Dec 3, 2026 •

Copy link
Copy Markdown
Owner

What and why

  • record_state failed the whole flush with ObjectDeletedError when an object's row was gone before its state could be loaded. The usual trigger is a delete of an object whose tracked collection is not loaded, raced by another transaction or preceded by SQL. Raising fails the request for a row that is already gone.
  • Now this follows AuditTrail(on_error=...). With "log" (the default) the entry is written without data.state and with data.state_missing = "row_gone", and a warning naming the model and the id is logged on audit_trail.listener. The other objects in the flush keep their full states, and a state is never partial. With "raise" the flush still raises ObjectDeletedError.
  • Objects expired by a commit already raise in plain SQLAlchemy, with or without the option. A test now pins that, and docs/guides/models.md says so.

Compatibility

  • state_missing is an ordinary, non-erasable data key. The integrity leaf keeps it like any other key, so it is tamper-evident. Rows without it commit and seal byte for byte as before (_block.py is unchanged). It is not a group, so a bundle whose only marker rows have no state stays version 1.
  • scrub() keeps it. Compaction takes it from the newest merged row, so a later full state wins. state_at still returns None for entity.deleted.

Tests

  • DB: deferred column, column expired on its own, and unloaded collection, each under both on_error modes. Also an orphan whose row is gone, a second object in the same flush keeping its full state, the warning, the marker surviving scrub, and state_at after a marked delete. An update of a gone row still raises StaleDataError, and an object expired by a commit raises in SQLAlchemy with and without the option.
  • Integrity: the marker verifies after a seal and a scrub, a bundle of it is version 1 and verifies, and removing it gives ROOT_MISMATCH.
  • Unit: compaction keeps the marker of the last row and drops an older one.

Review follow-up

  • The state keys of a flush (row_gone marker, member ids, loaded column keys) no longer outlive it. The session records which instances got them and drops them when the flush ends: in after_flush_postexec, in after_transaction_end of the flush's own subtransaction (a failed flush), and when loading raises in before_flush. Before, a failed flush could leave a stale row_gone marker or stale member ids that a later entity.created of the same object picked up. A created entry now also ignores both keys.
  • An update that only changes a tracked collection of a gone row runs no UPDATE of that row, so it gets state_missing like a delete (or raises with "raise"). The docs, the CHANGELOG and canonical.md now say so, and state_at raises ValueError for such an entry. The CHANGELOG names ActivityData.state_missing.
  • Tests: the failed-flush cases for the marker and for member ids (sync, asyncpg, psycopg async), a failure in before_flush, a failed flush inside a savepoint, the collection-only update under both modes, and log mode for every session kind.

With on_error="log" (the default), an object whose row is gone when
record_state loads its state (an unloaded tracked collection, a deferred
column or a column expired on its own) now gets its entry without
data.state and with data.state_missing = "row_gone", and a warning on
audit_trail.listener naming the model and the id. The other objects of
the flush keep their full states; a present state is always complete.
With on_error="raise" the flush still fails with ObjectDeletedError.

state_missing is an ordinary data key: scrub() keeps it, compaction takes
it from the newest row, and integrity seals it in the leaf, so rows
without it commit and seal byte for byte as before.
@WiktorB2004 WiktorB2004 self-assigned this Dec 3, 2026
The row_gone marker, the member ids and the loaded column keys that
load_for_state puts on InstanceState.info were cleared only by the next
load of that instance or by the entry that used them. A flush that failed
after marking an instance (StaleDataError, an IntegrityError of another
object) left them behind, so a later entity.created of the same object
could lose its state to a stale row_gone marker or get stale member ids
for an untouched collection.

The session now records the instances it gave keys to and drops the keys
when the flush ends: in after_flush_postexec, in after_transaction_end of
the flush's own subtransaction (a failed flush), and when loading itself
raises in before_flush. A created entry also ignores both keys, since
nothing is loaded for a new instance.

The docs no longer say an update of a gone row always fails: one that only
changes a tracked collection runs no UPDATE of the row, so its entry gets
state_missing too, and state_at raises ValueError for it. The CHANGELOG
names ActivityData.state_missing and that ValueError.
@WiktorB2004
WiktorB2004 merged commit 3446ee3 into main Dec 4, 2026
15 checks passed
@WiktorB2004
WiktorB2004 deleted the record-state-gone-row branch December 4, 2026 00:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant