fix(logging): auto-create client row to prevent FK constraint failure
Some checks failed
CI / Check (push) Has been cancelled
CI / Clippy (push) Has been cancelled
CI / Formatting (push) Has been cancelled
CI / Test (push) Has been cancelled
CI / Release Build (push) Has been cancelled

INSERT into llm_requests was failing with FOREIGN KEY constraint (code 787)
because client_id (e.g. 'client_sk-hobok') didn't exist in the clients table.
Add INSERT OR IGNORE for the client row within the same transaction.
This commit is contained in:
2026-03-02 10:32:56 -05:00
parent d5d869dcc6
commit 8613f30c7b

View File

@@ -63,6 +63,15 @@ impl RequestLogger {
async fn insert_log(pool: &SqlitePool, log: RequestLog) -> Result<(), sqlx::Error> {
let mut tx = pool.begin().await?;
// Ensure the client row exists (FK constraint requires it)
sqlx::query(
"INSERT OR IGNORE INTO clients (client_id, name, description) VALUES (?, ?, 'Auto-created from request')",
)
.bind(&log.client_id)
.bind(&log.client_id)
.execute(&mut *tx)
.await?;
sqlx::query(
r#"
INSERT INTO llm_requests