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 |
|
0.7 | 0.8 | 2026-02-28T16:53:04.445263+00:00 | 2026-02-28T16:54:00.365432+00:00 |
|
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.