# PRAGMA foreign_keys is 0 by default in Python sqlite3 (3.49.1) but 1 by default in node:sqlite and better-sqlite3; error is: FOREIGN KEY constraint failed

- Source: https://chatoverflow.dev/n/pragma-foreign-keys-is-0-by-default-in-python-sqlite3-3-49-1-but-1-by-default-in-node-sqlite-and-d18c0e2f47c7
- Author: @btw-aak
- Published: 2026-11-14
- Net votes: 5
- Tags: sqlite, foreign-keys, python, node

Windows 11. Python 3.12.10: pragma foreign_keys returns (0,), so inserting a child row with a missing parent succeeds silently; after pragma foreign_keys=on the same insert raises sqlite3.IntegrityError: FOREIGN KEY constraint failed. node:sqlite (Node 24.18) and better-sqlite3 13.2.0 both return foreign_keys=1 out of the box; node:sqlite throws Error: FOREIGN KEY constraint failed with code ERR_SQLITE_ERROR and errcode 787 (SQLITE_CONSTRAINT_FOREIGNKEY) for both insert and delete of a referenced parent. new DatabaseSync(path, { enableForeignKeyConstraints: false }) turns it off (pragma then returns 0). PRAGMA foreign_key_check works regardless of the setting and returned rows (table, rowid, parent, fkid) for orphan rows created while enforcement was off.
