Skip to content

Transform now refuses to run inside a transaction if destructive foreign keys exist#795

Merged
simonw merged 2 commits into
mainfrom
claude/sqlite-utils-issue-794-67snbh
Jul 12, 2026
Merged

Transform now refuses to run inside a transaction if destructive foreign keys exist#795
simonw merged 2 commits into
mainfrom
claude/sqlite-utils-issue-794-67snbh

Conversation

@simonw

@simonw simonw commented Jul 12, 2026

Copy link
Copy Markdown
Owner

…ign keys exist

PRAGMA foreign_keys cannot be changed inside a transaction, and the
defer_foreign_keys fallback only defers violation checks, not ON DELETE
actions - so calling table.transform() inside db.atomic() with
PRAGMA foreign_keys enabled would silently fire ON DELETE CASCADE /
SET NULL / SET DEFAULT actions on referencing tables when the old table
is dropped.

transform() now raises TransactionError in that situation, naming the
offending foreign keys and suggesting either running it outside the
transaction or turning the pragma off before opening it. Tables with
only non-destructive inbound foreign keys (NO ACTION, RESTRICT) can
still be transformed inside a transaction via defer_foreign_keys.

Closes #794

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014StVTWQJpFhfZJK2CYVBwv
@codecov

codecov Bot commented Jul 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.55%. Comparing base (d714200) to head (4de0fe1).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #795   +/-   ##
=======================================
  Coverage   95.55%   95.55%           
=======================================
  Files           9        9           
  Lines        3798     3802    +4     
=======================================
+ Hits         3629     3633    +4     
  Misses        169      169           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread docs/python-api.rst Outdated

.. _python_api_transform_foreign_keys_transactions:

Foreign keys and transactions

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the Foreign keys and transactions section to the bottom of the transform section, not the top where it is right now

@simonw

simonw commented Jul 12, 2026

Copy link
Copy Markdown
Owner Author

@simonw simonw merged commit f66ddcb into main Jul 12, 2026
106 checks passed
@simonw simonw deleted the claude/sqlite-utils-issue-794-67snbh branch July 12, 2026 15:43
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.

2 participants