claude-memory/graph/fixes/fix-sqlite-pragma-foreign-keys-is-per-connection-not-per-poo-49a800.md
2026-02-28 10:54:00 -06:00

1.7 KiB

id type title tags importance confidence created updated relations
49a8001d-b3b9-4a29-911a-956db4b37f21 fix Fix: SQLite PRAGMA foreign_keys is per-connection, not per-pool
sba-scout
rust
sqlx
sqlite
fix
pragma
connection-pool
0.7 0.8 2026-02-28T16:53:04.445263+00:00 2026-02-28T16:54:00.365432+00:00
target type direction strength edge_id
c9add129-283a-445a-8dff-4525ec7fa9b6 RELATED_TO outgoing 0.8 38d8bd6e-af74-4d93-9c35-709865f12f2f
target type direction strength edge_id
bc4abc6e-57c1-4d6c-b414-213c1367be9b RELATED_TO outgoing 0.7 6f4bc010-3d87-484c-b762-e700036a6b1f

SQLite PRAGMA Scope with sqlx Connection Pools

Project: sba-scout (Rust)

Problem

When using sqlx connection pools, PRAGMA statements only affect the specific connection they run on. Running PRAGMA foreign_keys = OFF via pool.execute() may use a different connection than subsequent queries — so FK enforcement remains on for those queries.

Fix

Use SqliteConnectOptions::foreign_keys(false) to apply the setting to every connection the pool creates, not just whichever one happens to handle the PRAGMA execute call.

let opts = SqliteConnectOptions::from_str(&db_url)?
    .foreign_keys(false)
    .create_if_missing(true);

let pool = SqlitePool::connect_with(opts).await?;

Context

For sba-scout, FK enforcement was disabled entirely because transactions reference players/teams that may not exist locally (data arrives out of order from the API).

General Rule

Any SQLite session-level setting (PRAGMA) must be applied at the connection options level when using a pool, not via a one-off execute() call.