{"record":{"id":"31a5490e5820be59","repo":"nautechsystems/nautilus_trader","slug":"failed-to-load-currencies-e","errorCode":null,"errorMessage":"Failed to load currencies: {e}","messagePattern":"Failed to load currencies: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/sql/queries.rs","lineNumber":128,"sourceCode":"            .bind(currency.name.as_str())\n            .bind(CurrencyTypePg(currency.currency_type))\n            .execute(pool)\n            .await\n            .map(|_| ())\n            .map_err(|e| anyhow::anyhow!(\"Failed to insert into currency table: {e}\"))\n    }\n\n    /// Loads all `Currency` entries via the provided `pool`.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the SELECT operation fails.\n    pub async fn load_currencies(pool: &PgPool) -> anyhow::Result<Vec<Currency>> {\n        sqlx::query_as::<_, CurrencyRow>(\"SELECT * FROM currency ORDER BY id ASC\")\n            .fetch_all(pool)\n            .await\n            .map(|rows| rows.into_iter().map(|row| row.0).collect())\n            .map_err(|e| anyhow::anyhow!(\"Failed to load currencies: {e}\"))\n    }\n\n    /// Loads a single `Currency` entry by `code` via the provided `pool`.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the SELECT operation fails.\n    pub async fn load_currency(pool: &PgPool, code: &str) -> anyhow::Result<Option<Currency>> {\n        sqlx::query_as::<_, CurrencyRow>(\"SELECT * FROM currency WHERE id = $1\")\n            .bind(code)\n            .fetch_optional(pool)\n            .await\n            .map(|currency| currency.map(|row| row.0))\n            .map_err(|e| anyhow::anyhow!(\"Failed to load currency: {e}\"))\n    }\n\n    /// Inserts or updates an `InstrumentAny` entry via the provided `pool`.\n    ///","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/sql/queries.rs#L110-L146","documentation":"`DatabaseQueries::load_currencies` runs `SELECT * FROM currency ORDER BY id ASC`, decodes each row into a `Currency`, and wraps any sqlx failure in this anyhow error. Failure means execution or decoding failed: connectivity, missing table, or rows containing `currency_type` enum values written by a different version that this binary's decoder cannot understand.","triggerScenarios":"Calling `DatabaseQueries::load_currencies(pool)` when the connection fails, the `currency` table doesn't exist, or a CurrencyRow fails to decode (e.g. a newer enum variant in the DB that CurrencyTypePg in this version rejects).","commonSituations":"Version skew: currencies persisted by a newer Nautilus release, then read by an older binary at startup; fresh database without migrations; transient network errors during fetch.","solutions":["Run matching migrations so the `currency` table and enum values align with this code version.","Align writer/reader versions so all stored currency_type values are decodable, or re-write offending rows with the current version.","Check DATABASE_URL points at the intended cache database.","Read the inner sqlx error to distinguish 'relation does not exist' (migrations) from 'error decoding' (data/schema drift)."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"let exists: bool = sqlx::query_scalar(\n    \"SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'currency')\",\n).fetch_one(pool).await?;\nif !exists {\n    return Err(anyhow::anyhow!(\"'currency' table missing — run migrations\"));\n}","typeGuard":null,"tryCatchPattern":"match DatabaseQueries::load_currencies(&pool).await {\n    Err(e) if e.to_string().contains(\"error decoding\") => {\n        return Err(anyhow::anyhow!(\n            \"currency rows unreadable by this version — align writer/reader versions: {e}\"\n        ));\n    }\n    result => result?,\n}","preventionTips":["Keep writer and reader application versions aligned so all stored enum values decode.","Run migrations before loading currencies from a cache database.","Verify DATABASE_URL targets the intended cache database.","Classify failures from the inner sqlx error (missing relation vs decode error)."],"tags":["postgres","sqlx","database","select","currency","enum"],"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"}