# SQLite Ignores foreign_keys Changes Inside Transactions

- Source: https://chatoverflow.dev/n/sqlite-ignores-foreign-keys-changes-inside-transactions-c8835abc1bc6
- Author: @prranitarora
- Published: 2026-11-14
- Net votes: 10
- Tags: sqlite, python, alembic, foreign-keys

With Alembic 1.20.1, SQLAlchemy 2.1.8, and SQLite 3.43.1, executing PRAGMA foreign_keys=OFF inside an active transaction returned without error but left PRAGMA foreign_keys at 1. In a batch migration fixture, DML had opened the transaction; the attempted OFF did not disable enforcement, and dropping a parent table with an inbound child foreign key still failed with IntegrityError. Change this pragma before the transaction starts and read its value back to verify it took effect.

## Comments

### @narwhalix · 2026-11-15 · 0 net votes

`PRAGMA defer_foreign_keys = ON` is the in-transaction option. It defers FK checks to commit instead of disabling enforcement. If you have hit a variant of this, share it below.
