fix: suppress UPDATE callbacks for objects deleted in the same transaction (#205)

This commit is contained in:
d3vyce
2026-03-31 21:40:18 +02:00
committed by GitHub
parent ebaa61525f
commit 5c1487c24a
2 changed files with 26 additions and 0 deletions

View File

@@ -231,6 +231,13 @@ class EventSession(AsyncSession):
k: v for k, v in field_changes.items() if k not in transient_ids
}
# Suppress updates for deleted objects (row is gone, refresh would fail).
if deletes and field_changes:
deleted_ids = {id(o) for o, _ in deletes}
field_changes = {
k: v for k, v in field_changes.items() if k not in deleted_ids
}
# Suppress updates for newly created objects (CREATE-only semantics).
if creates and field_changes:
create_ids = {id(o) for o in creates}

View File

@@ -1041,6 +1041,25 @@ class TestTransientObject:
assert len(creates) == 1
assert len(deletes) == 1
@pytest.mark.anyio
async def test_update_then_delete_suppresses_update_callback(self, mixin_session):
"""UPDATE callback is suppressed when the object is also deleted in the same transaction."""
obj = WatchedModel(status="initial", other="x")
mixin_session.add(obj)
await mixin_session.commit()
_test_events.clear()
obj.status = "changed"
await mixin_session.flush()
await mixin_session.delete(obj)
await mixin_session.commit()
updates = [e for e in _test_events if e["event"] == "update"]
deletes = [e for e in _test_events if e["event"] == "delete"]
assert updates == []
assert len(deletes) == 1
class TestPolymorphism:
"""Event dispatch with STI (Single Table Inheritance)."""