Skip to content

db: bulk-DML audit prefetch drops with_loader_criteria and records rows the statement did not touch #810

Closed

Description

@antosubash

Summary

Bulk-DML auditing prefetches the affected IDs with a SELECT rebuilt from stmt.whereclause. That SELECT drops statement options such as with_loader_criteria, which SQLAlchemy does apply to the UPDATE/DELETE itself. So the audit log can record bulk_updated/bulk_deleted entries for rows the statement never touched. This contradicts docs/modules/audit_log.md:40.

Where

framework/db/simple_module_db/bulk_audit.py:125 (_prefetch_ids)

Reproduction

With two rows (value 1 and value 2), run update(AuditTestItem).options(with_loader_criteria(AuditTestItem, AuditTestItem.value == 2)).values(name="changed"). The rowcount is 1, but audit records are written for both IDs.

Suggested fix

Build the prefetch SELECT with the statement's _with_options / loader criteria carried over, or use RETURNING where the dialect supports it.

Found in a full review of simple_module_db (main @ 7a5c860).

Activity

  1. antosubash commented on Nov 4, 2026

    @antosubash
    OwnerAuthor

    Fixed in #821 (squash-merged as 0fcd877).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions