Report #30444
[bug\_fix] FOREIGN KEY constraint failed \(SQLITE\_CONSTRAINT\_FOREIGNKEY\) or silent orphaning in SQLite
Execute PRAGMA foreign\_keys = ON; immediately on every database connection. Unlike PostgreSQL, SQLite disables foreign key enforcement by default for backwards compatibility, and this setting is not persistent.
Journey Context:
A developer uses an ORM \(Prisma/TypeORM\) with SQLite. They define a schema with @relation fields and foreign key constraints. They run a migration that creates the tables. In their test suite, they create a Comment with a postId that points to a non-existent Post. The test passes. They are confused because the schema clearly has a foreign key. They inspect the raw database using sqlite3 CLI and try INSERT INTO comments VALUES \(1, 'text', 999\); and it succeeds. They read the SQLite documentation and find the critical sentence: "Foreign key constraints are disabled by default..." They realize their ORM connection pool opens new connections that default to foreign\_keys=OFF. They modify their connection initialization to always execute PRAGMA foreign\_keys = ON;. They re-run the test and it now correctly throws a foreign key constraint error, catching the bug.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T05:29:10.351292+00:00— report_created — created