{"record":{"id":"5359b545631a33ad","repo":"nautechsystems/nautilus_trader","slug":"failed-to-insert-into-token-table-e","errorCode":null,"errorMessage":"Failed to insert into token table: {e}","messagePattern":"Failed to insert into token table: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/adapters/blockchain/src/cache/database.rs","lineNumber":1163,"sourceCode":"                chain_id, address, name, symbol, decimals\n            ) VALUES ($1, $2, $3, $4, $5)\n            ON CONFLICT (chain_id, address)\n            DO UPDATE\n            SET\n                name = $3,\n                symbol = $4,\n                decimals = $5\n        \",\n        )\n        .bind(token.chain.chain_id as i32)\n        .bind(token.address.to_string())\n        .bind(token.name.as_str())\n        .bind(token.symbol.as_str())\n        .bind(i32::from(token.decimals))\n        .execute(&self.pool)\n        .await\n        .map(|_| ())\n        .map_err(|e| anyhow::anyhow!(\"Failed to insert into token table: {e}\"))\n    }\n\n    /// Records an invalid token address with associated error information.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the database insertion fails.\n    pub async fn add_invalid_token(\n        &self,\n        chain_id: u32,\n        address: &Address,\n        error_string: &str,\n    ) -> anyhow::Result<()> {\n        sqlx::query(\n            \"\n            INSERT INTO token (\n                chain_id, address, error\n            ) VALUES ($1, $2, $3)","sourceCodeStart":1145,"sourceCodeEnd":1181,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/blockchain/src/cache/database.rs#L1145-L1181","documentation":"Database write error in the blockchain cache's token insert: the SQLx upsert of a token row (chain_id, address, name, symbol, decimals) into PostgreSQL failed; the driver error is embedded in the message.","triggerScenarios":"Calling the token insert when `token.name` or `token.symbol` exceed column length limits (VARCHAR overflow), `decimals` doesn't fit i32 (it is cast with i32::from(u8) so unlikely), the token address violates a unique constraint/cast, or the pool connection fails during execute.","commonSituations":"On-chain token metadata with very long names/symbols overflowing VARCHAR(n); replaying token discovery duplicating the address key; fresh database missing the token table; DB role without INSERT privilege.","solutions":["Read the wrapped `{e}`: 'value too long' → truncate/validate name and symbol against column limits before insert","Use ON CONFLICT (address) DO UPDATE or dedupe tokens before upsert for replay safety","Run migrations to ensure the token table matches the code's expected schema","Confirm DB connectivity and INSERT privileges for the configured role"],"exampleFix":"// before\n.bind(token.name.as_str()) // may exceed VARCHAR(64)\n// after\n.bind(token.name.chars().take(64).collect::<String>())","handlingStrategy":"validation","validationCode":"const MAX_NAME: usize = 64; // match the column limit\nanyhow::ensure!(!token.address.is_empty(), \"token address empty\");\nanyhow::ensure!(token.name.chars().count() <= MAX_NAME && token.symbol.chars().count() <= 32, \"token metadata exceeds column length\");","typeGuard":"fn token_valid(t: &Token) -> bool {\n    !t.address.is_empty()\n        && t.name.chars().count() <= 64\n        && t.symbol.chars().count() <= 32\n}","tryCatchPattern":"if let Err(e) = db.insert_token(&token).await {\n    if e.to_string().contains(\"value too long\") {\n        let mut t = token.clone(); t.name.truncate(64);\n        db.insert_token(&t).await?;\n    } else { return Err(e); }\n}","preventionTips":["Truncate or validate name/symbol against column limits — on-chain metadata is unbounded","Use upsert semantics keyed on address so rediscovery is idempotent","Run migrations before token discovery writes","Escape/handle multi-byte characters safely (sqlx handles this; avoid manual SQL concatenation)"],"tags":["database","postgres","sqlx","insert"],"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-14T00:17:10.932Z"}