{"record":{"id":"bbbf7ec6c49280e5","repo":"diesel-rs/diesel","slug":"failed-to-convert-bigdecimal-to-pgnumeric","errorCode":null,"errorMessage":"Failed to convert BigDecimal to PgNumeric","messagePattern":"Failed to convert BigDecimal to PgNumeric","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"diesel/src/pg/types/numeric.rs","lineNumber":92,"sourceCode":"    #[cfg(all(feature = \"postgres_backend\", feature = \"numeric\"))]\n    impl TryFrom<PgNumeric> for BigDecimal {\n        type Error = Box<dyn Error + Send + Sync>;\n\n        fn try_from(numeric: PgNumeric) -> deserialize::Result<Self> {\n            (&numeric).try_into()\n        }\n    }\n\n    // that should likely be a `TryFrom` impl\n    // TODO: diesel 3.0\n    // This now mostly exists for backward compatibility\n    // Our own `ToSql` impls don't call it anymore in favour of calling\n    // the failable inner function instead\n    #[cfg(all(feature = \"postgres_backend\", feature = \"numeric\"))]\n    impl<'a> From<&'a BigDecimal> for PgNumeric {\n        fn from(decimal: &'a BigDecimal) -> Self {\n            try_convert_decimal_to_pg_numeric(decimal)\n                .expect(\"Failed to convert BigDecimal to PgNumeric\")\n        }\n    }\n\n    // NOTE(clippy): No `std::ops::MulAssign` impl for `BigInt`\n    // NOTE(clippy): Clippy suggests to replace the `.take_while(|i| i.is_zero())`\n    // with `.take_while(Zero::is_zero)`, but that's a false positive.\n    // The closure gets an `&&i16` due to autoderef `<i16 as Zero>::is_zero(&self) -> bool`\n    // is called. There is no impl for `&i16` that would work with this closure.\n    #[allow(clippy::assign_op_pattern, clippy::redundant_closure)]\n    #[cfg(all(feature = \"postgres_backend\", feature = \"numeric\"))]\n    fn try_convert_decimal_to_pg_numeric(\n        decimal: &BigDecimal,\n    ) -> Result<PgNumeric, Box<dyn core::error::Error + Send + Sync>> {\n        let (mut integer, scale) = decimal.as_bigint_and_exponent();\n\n        // Handling of negative scale\n        let scale = if scale < -131064 {\n            // that's a guard avoiding the potential expensive calculation below","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/diesel-rs/diesel/blob/6fa6ed01b24b24248ab2a611698d0a7c6a2e9120/diesel/src/pg/types/numeric.rs#L74-L110","documentation":"Runtime panic from the deprecated From<BigDecimal> for PgNumeric conversion (kept for backward compatibility; modern ToSql impls use a failable TryFrom-style path instead). Converting an arbitrary BigDecimal into PgNumeric can fail (e.g. digits that do not fit the base-10000 representation), and this legacy infallible API panics with this message in that case. Fix: migrate to the failable conversion path instead of the legacy From impl.","triggerScenarios":"Thrown at diesel/src/pg/types/numeric.rs:92 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Reduce the scale or digit count of the BigDecimal","Normalize the value before conversion","Handle unsupported precision with a custom ToSql implementation"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"6fa6ed01b24b24248ab2a611698d0a7c6a2e9120","analyzedAt":"2026-09-07T01:50:13.074Z","contentChangedAt":"2026-09-07T01:50:13.074Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}