{"record":{"id":"86ca59af06d61aeb","repo":"nautechsystems/nautilus_trader","slug":"failed-to-insert-into-custom-table-e","errorCode":null,"errorMessage":"Failed to insert into custom table: {e}","messagePattern":"Failed to insert into custom table: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/sql/queries.rs","lineNumber":1669,"sourceCode":"                updated_at = CURRENT_TIMESTAMP\n        \"#,\n        )\n        .bind(data_type_name)\n        .bind(&metadata_json)\n        .bind(identifier)\n        .bind(&value_json)\n        .bind(\n            value_json\n                .get(\"ts_event\")\n                .and_then(serde_json::Value::as_u64)\n                .unwrap_or_else(|| data.ts_init().as_u64())\n                .to_string(),\n        )\n        .bind(data.ts_init().to_string())\n        .execute(pool)\n        .await\n        .map(|_| ())\n        .map_err(|e| anyhow::anyhow!(\"Failed to insert into custom table: {e}\"))\n    }\n\n    /// Loads all `CustomData` entries of `data_type` via the provided `pool`.\n    ///\n    /// Filters by `data_type`, `metadata`, and `identifier` columns to match the requested data type.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the SQL SELECT or deserialization fails.\n    pub async fn load_custom_data(\n        pool: &PgPool,\n        data_type: &DataType,\n    ) -> anyhow::Result<Vec<CustomData>> {\n        let metadata_json = data_type.metadata().as_ref().map_or(\n            Ok(serde_json::Value::Object(serde_json::Map::new())),\n            serde_json::to_value,\n        )?;\n","sourceCodeStart":1651,"sourceCodeEnd":1687,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/sql/queries.rs#L1651-L1687","documentation":"Raised by `add_custom_data` when the INSERT into the `custom` table fails. All JSON extraction steps succeeded; the error comes from sqlx executing the INSERT against PostgreSQL, wrapped with this message via anyhow.","triggerScenarios":"Calling `add_custom_data(pool, data)` when the `custom` table doesn't exist, columns mismatch (e.g., missing data_type/metadata/identifier columns), a constraint or type check fails on the JSONB value, or the connection dies during execute.","commonSituations":"Schema migrations not applied or out of date; inserting a data type whose identifier column value violates a unique constraint; read-only database; pool connection closed.","solutions":["Read the embedded sqlx error to find the exact DB-level cause.","Apply the latest migrations so the `custom` table has all expected columns.","Verify identifier/uniqueness constraints aren't violated by the data_type/identifier combination.","Check database writability and pool health."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"let exists: bool = sqlx::query_scalar(\n    \"SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'custom')\")\n    .fetch_one(pool).await?;\nanyhow::ensure!(exists, \"custom table missing; run migrations\");","typeGuard":null,"tryCatchPattern":"if let Err(e) = add_custom_data(&pool, &data).await {\n    tracing::error!(\"custom insert failed: {e:#}\");\n    return Err(e.context(\"custom data persistence failed\"));\n}","preventionTips":["Keep schema migrations current with library version","Check unique constraints on data_type/identifier","Ensure DB is writable","Log the full anyhow chain for root cause"],"tags":["database","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-14T05:17:10.506Z"}