{"record":{"id":"05bc30e1796e96d0","repo":"nautechsystems/nautilus_trader","slug":"failed-to-insert-into-client-table-e","errorCode":null,"errorMessage":"Failed to insert into client table: {e}","messagePattern":"Failed to insert into client table: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/sql/queries.rs","lineNumber":653,"sourceCode":"        .bind(order_event.trader_id().to_string())\n        .execute(&mut *transaction)\n        .await\n        .map(|_| ())\n        .map_err(|e| anyhow::anyhow!(\"Failed to insert into trader table: {e}\"))?;\n\n        // Insert client if it does not exist\n        // TODO remove this when client initialization is implemented\n        if let Some(client_id) = client_id {\n            sqlx::query(\n                r#\"\n                INSERT INTO \"client\" (id) VALUES ($1) ON CONFLICT (id) DO NOTHING\n            \"#,\n            )\n            .bind(client_id.to_string())\n            .execute(&mut *transaction)\n            .await\n            .map(|_| ())\n            .map_err(|e| anyhow::anyhow!(\"Failed to insert into client table: {e}\"))?;\n        }\n\n        let exec_algorithm_params = order_event\n            .exec_algorithm_params()\n            .map(serde_json::to_value)\n            .transpose()\n            .map_err(|e| anyhow::anyhow!(\"Failed to serialize exec algorithm params: {e}\"))?;\n        let info = order_event\n            .info()\n            .map(serde_json::to_value)\n            .transpose()\n            .map_err(|e| anyhow::anyhow!(\"Failed to serialize order event info: {e}\"))?;\n\n        sqlx::query(r#\"\n            INSERT INTO \"order_event\" (\n                id, kind, client_order_id, order_type, order_side, trader_id, client_id, reason, strategy_id, instrument_id, trade_id, currency, quantity, time_in_force, liquidity_side,\n                post_only, reduce_only, quote_quantity, reconciliation, price, last_px, last_qty, trigger_price, trigger_type, limit_offset, trailing_offset,\n                trailing_offset_type, expire_time, display_qty, emulation_trigger, trigger_instrument_id, contingency_type,","sourceCodeStart":635,"sourceCodeEnd":671,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/sql/queries.rs#L635-L671","documentation":"Wraps failure of the INSERT into the client table performed by `add_order_event` when a `client_id` is supplied (a temporary step until client initialization is implemented). This runs inside the same transaction, so any failure rolls back the trader insert as well. Typically caused by constraint violations or connectivity issues on the client table.","triggerScenarios":"Calling `add_order_event` with `Some(client_id)` when the client table is missing, the client row violates a unique/constraint rule, or the connection fails mid-transaction.","commonSituations":"Schema not migrated; duplicate client_id with conflicting columns; database failover during write; client_id string format mismatched with the column type.","solutions":["Inspect `{e}` for the exact sqlx cause (unique violation vs connection).","Run migrations so the client table exists with the expected schema.","Ensure the client_id string form matches what the schema expects.","Retry `add_order_event` — the transaction rolls back cleanly on failure."],"exampleFix":"// before\nadd_order_event(&mut tx, &event, Some(client_id.clone())).await?;\n// after\nadd_order_event(&mut tx, &event, Some(client_id.clone())).await\n    .with_context(|| format!(\"persisting event for client {client_id}\"))?;","handlingStrategy":"try-catch","validationCode":"sqlx::query(r#\"SELECT EXISTS(SELECT 1 FROM \"client\" WHERE id = $1)\"#)\n    .bind(client_id.to_string())\n    .fetch_one(pool).await\n    .map_err(|e| anyhow::anyhow!(\"client table unavailable: {e}\"))?;","typeGuard":"fn is_duplicate_client(e: &anyhow::Error) -> bool {\n    e.to_string().contains(\"duplicate key\")\n}","tryCatchPattern":"match add_order_event(&mut tx, &event, Some(client_id.clone())).await {\n    Ok(()) => {}\n    Err(e) if is_duplicate_client(&e) => { /* client already registered — safe to continue */ }\n    Err(e) => return Err(e),\n}","preventionTips":["Pre-register clients during initialization instead of relying on add_order_event.","Verify the client table schema before deployments.","Keep client_id formatting canonical (uppercase/lowercase consistently)."],"tags":["database","sqlx","transaction","postgres"],"backgroundTag":"database-write-failed","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}