{"record":{"id":"f6fa91ac9321c691","repo":"nautechsystems/nautilus_trader","slug":"account-event-does-not-exist-for-account","errorCode":null,"errorMessage":"Account event does not exist for account: {}","messagePattern":"Account event does not exist for account: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/sql/queries.rs","lineNumber":1101,"sourceCode":"        })\n    }\n\n    /// Inserts or updates an `AccountState` event via the provided `pool`.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the SQL INSERT or UPDATE operation fails.\n    pub async fn add_account(\n        pool: &PgPool,\n        updated: bool,\n        account_event: AccountState,\n    ) -> anyhow::Result<()> {\n        if updated {\n            let exists =\n                Self::check_if_account_event_exists(pool, account_event.account_id).await?;\n\n            if !exists {\n                anyhow::bail!(\n                    \"Account event does not exist for account: {}\",\n                    account_event.account_id\n                );\n            }\n        }\n\n        let mut transaction = pool.begin().await?;\n        let event = serde_json::to_value(&account_event)\n            .map_err(|e| anyhow::anyhow!(\"Failed to serialize account event: {e}\"))?;\n        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","sourceCodeStart":1083,"sourceCodeEnd":1119,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/sql/queries.rs#L1083-L1119","documentation":"Thrown by `add_account` when it attempted to UPDATE an existing account event (`updated == true`) but `check_if_account_event_exists` reports that no event row exists for the account. The code is trying to update a record that is not present in the database, so it refuses to proceed.","triggerScenarios":"Calling `AccountQueries::add_account(pool, account_event)` with an event whose account_id is not yet persisted while the code path determines the event should update an existing row (e.g. a stale in-memory cache believes the event exists, but the DB row was deleted or belongs to a different database).","commonSituations":"Pointing the node at a fresh/empty database while reusing an old cache snapshot; someone manually deleted rows from the account events table; using separate databases for write and read pools.","solutions":["Verify the account event row exists in the database for that account_id (SELECT from the account events table).","If the record should be new, clear/rebuild the local cache so `updated` is false and the event is INSERTed instead of UPDATEd.","Confirm the process is connected to the intended database/schema; re-add the account so the initial event is persisted first."],"exampleFix":"// before: updating assuming the event exists\nqueries.add_account(&pool, updated_event).await?;\n\n// after: ensure the account is initialized first, or insert when absent\nif !account_event_exists(&pool, &account_id).await? {\n    queries.add_account(&pool, initial_event).await?; // INSERT\n}\nqueries.add_account(&pool, updated_event).await?;","handlingStrategy":"validation","validationCode":"let exists = sqlx::query_scalar::<_, bool>(\n    \"SELECT EXISTS(SELECT 1 FROM account_events WHERE account_id = $1)\",\n)\n.bind(account_event.account_id.to_string())\n.fetch_one(&pool)\n.await?;\nif !exists {\n    // insert the initial event before any update\n}","typeGuard":null,"tryCatchPattern":"if let Err(e) = AccountQueries::add_account(&pool, &event).await {\n    if e.to_string().contains(\"does not exist for account\") {\n        // re-persist the account's initial state, then retry\n    } else { return Err(e); }\n}","preventionTips":["Never delete account event rows manually; use the library's flush mechanisms.","Ensure all nodes share the same database — don't split read/write pools.","Initialize accounts (persist their first event) before applying updates."],"tags":["database","record-not-found","state-mismatch","rust"],"backgroundTag":"record-not-found","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"}