{"record":{"id":"ad60c54ea2d9c3fd","repo":"nautechsystems/nautilus_trader","slug":"failed-to-check-if-order-initialized-exists-e","errorCode":null,"errorMessage":"Failed to check if order initialized exists: {e}","messagePattern":"Failed to check if order initialized exists: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/sql/queries.rs","lineNumber":587,"sourceCode":"    }\n\n    /// Checks if an `OrderInitialized` event exists for the given `client_order_id` via the provided `pool`.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the SQL SELECT operation fails.\n    pub async fn check_if_order_initialized_exists(\n        pool: &PgPool,\n        client_order_id: ClientOrderId,\n    ) -> anyhow::Result<bool> {\n        sqlx::query(r#\"\n            SELECT EXISTS(SELECT 1 FROM \"order_event\" WHERE client_order_id = $1 AND kind = 'OrderInitialized')\n        \"#)\n            .bind(client_order_id.to_string())\n            .fetch_one(pool)\n            .await\n            .map(|row| row.get(0))\n            .map_err(|e| anyhow::anyhow!(\"Failed to check if order initialized exists: {e}\"))\n    }\n\n    /// Checks if any account event exists for the given `account_id` via the provided `pool`.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the SQL SELECT operation fails.\n    pub async fn check_if_account_event_exists(\n        pool: &PgPool,\n        account_id: AccountId,\n    ) -> anyhow::Result<bool> {\n        sqlx::query(\n            r#\"\n            SELECT EXISTS(SELECT 1 FROM \"account_event\" WHERE account_id = $1)\n        \"#,\n        )\n        .bind(account_id.to_string())\n        .fetch_one(pool)","sourceCodeStart":569,"sourceCodeEnd":605,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/sql/queries.rs#L569-L605","documentation":"Wraps sqlx failures from the `SELECT EXISTS(...)` query that checks whether an `OrderInitialized` event row exists for a `client_order_id` in the `order_event` table. The library converts the driver error into anyhow context so callers can distinguish a query failure from a simple `false` answer. Note the boolean result is always `Ok` when the query succeeds; this error only fires when the query itself cannot be executed.","triggerScenarios":"Calling `check_if_order_initialized_exists(pool, client_order_id)` with an unreachable database, a missing/renamed `order_event` table or `kind` column, or a client_order_id whose string form is incompatible with the column type.","commonSituations":"Schema not migrated before first use; connecting to the wrong database or schema; connection pool exhausted after idle timeout; case-sensitivity issues with the quoted `\"order_event\"` table name.","solutions":["Check DB connectivity with a trivial query on the same pool.","Confirm the `order_event` table exists and has `client_order_id` and `kind` columns (run migrations).","Verify the `client_order_id.to_string()` matches the stored id format.","Read the inner sqlx error in `{e}` for the exact driver cause and retry if transient."],"exampleFix":"// before\nlet exists = check_if_order_initialized_exists(&pool, client_order_id).await?;\n// after\nlet exists = check_if_order_initialized_exists(&pool, client_order_id)\n    .await\n    .with_context(|| format!(\"checking init for {client_order_id}\"))?;","handlingStrategy":"try-catch","validationCode":"// Ensure table exists before checking\nsqlx::query(r#\"SELECT EXISTS(SELECT 1 FROM \"order_event\" LIMIT 1)\"#)\n    .fetch_one(pool).await\n    .map_err(|e| anyhow::anyhow!(\"order_event table unavailable: {e}\"))?;","typeGuard":"fn is_schema_error(e: &anyhow::Error) -> bool {\n    let s = e.to_string();\n    s.contains(\"does not exist\") || s.contains(\"relation\") || s.contains(\"column\")\n}","tryCatchPattern":"match check_if_order_initialized_exists(&pool, client_order_id).await {\n    Ok(exists) => { /* exists is a plain bool */ }\n    Err(e) if is_schema_error(&e) => { /* run migrations, then retry */ }\n    Err(e) => return Err(e),\n}","preventionTips":["Apply migrations as part of deployment startup.","Keep the reader crate and DB schema on the same release.","Treat the boolean false as a normal result; only errors are failures."],"tags":["database","sqlx","postgres","existence-check"],"backgroundTag":"database-query-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"}