From 8613f30c7b065abca4e2b0bb5d69a0fd850c60b7 Mon Sep 17 00:00:00 2001 From: hobokenchicken Date: Mon, 2 Mar 2026 10:32:56 -0500 Subject: [PATCH] fix(logging): auto-create client row to prevent FK constraint failure 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. --- src/logging/mod.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/logging/mod.rs b/src/logging/mod.rs index 0b20f5f8..d80fdd10 100644 --- a/src/logging/mod.rs +++ b/src/logging/mod.rs @@ -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