{"record":{"id":"36e475fdcde7b574","repo":"nautechsystems/nautilus_trader","slug":"failed-to-insert-into-account-table-e","errorCode":null,"errorMessage":"Failed to insert into account table: {e}","messagePattern":"Failed to insert into account table: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/sql/queries.rs","lineNumber":1129,"sourceCode":"        let balances = event\n            .get(\"balances\")\n            .cloned()\n            .ok_or_else(|| anyhow::anyhow!(\"Serialized account event has no balances\"))?;\n        let margins = event\n            .get(\"margins\")\n            .cloned()\n            .ok_or_else(|| anyhow::anyhow!(\"Serialized account event has no margins\"))?;\n\n        sqlx::query(\n            r#\"\n            INSERT INTO \"account\" (id) VALUES ($1) ON CONFLICT (id) DO NOTHING\n        \"#,\n        )\n        .bind(account_event.account_id.to_string())\n        .execute(&mut *transaction)\n        .await\n        .map(|_| ())\n        .map_err(|e| anyhow::anyhow!(\"Failed to insert into account table: {e}\"))?;\n\n        sqlx::query(r#\"\n            INSERT INTO \"account_event\" (\n                id, kind, account_id, base_currency, balances, margins, is_reported, ts_event, ts_init, created_at, updated_at\n            ) VALUES (\n                $1, $2, $3, $4, $5, $6, $7, $8, $9, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP\n            )\n            ON CONFLICT (id)\n            DO UPDATE\n            SET\n                kind = $2, account_id = $3, base_currency = $4, balances = $5, margins = $6, is_reported = $7,\n                ts_event = $8, ts_init = $9, updated_at = CURRENT_TIMESTAMP\n        \"#)\n            .bind(account_event.event_id.to_string())\n            .bind(account_event.account_type.to_string())\n            .bind(account_event.account_id.to_string())\n            .bind(account_event.base_currency.map(|x| x.code.as_str()))\n            .bind(balances)","sourceCodeStart":1111,"sourceCodeEnd":1147,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/sql/queries.rs#L1111-L1147","documentation":"add_account upserts a row into the \"account\" table (INSERT ... ON CONFLICT DO NOTHING) before writing the account_event row. Failure of this INSERT is wrapped with this message. As with the trader insert, the cause is DB-level: connectivity, missing schema, or privileges.","triggerScenarios":"Calling add_account when the INSERT INTO \"account\" fails — no \"account\" table (migrations not applied), connection failure, or insufficient INSERT privilege.","commonSituations":"Fresh database without migrations run; Postgres restarted or idle connection reaped mid-call; DB role granted rights only on event tables; connecting to the wrong database/tenant schema.","solutions":["Check the embedded sqlx error: 'relation \"account\" does not exist' means apply migrations.","Verify connection string, network reachability, and pool health.","Grant INSERT on the account table to the application's DB role.","Retry transient connection errors with backoff; the open transaction is rolled back automatically on drop."],"exampleFix":"// before: failing silently deep inside add_account\npsql -c \"\\dt\"  # account table missing\n\n// after: apply schema before use\nsqlx migrate run  # or apply the crate's SQL schema, then retry add_account","handlingStrategy":"try-catch","validationCode":"// preflight table existence and privilege\nsqlx::query(\"SELECT to_regclass('\\\"account\\\"') IS NOT NULL AND has_table_privilege(current_user, '\\\"account\\\"', 'INSERT')\")\n    .fetch_one(pool).await?;","typeGuard":null,"tryCatchPattern":"match add_account(&pool, updated, state).await {\n    Err(e) if is_transient(&e) => retry_with_backoff(|| add_account(&pool, updated, state.clone())).await?,\n    Err(e) => return Err(e),\n    Ok(()) => {},\n}","preventionTips":["Run migrations at deploy time; fail fast if the account table is absent.","Grant the app DB role INSERT on account and account_event.","Validate the connection URL points at the intended database/schema.","Enable pool health checks and TCP keepalives."],"tags":["database","postgres","sqlx","account"],"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"}