diesel-rs/diesel · error

underflow subtracting money amounts

Error message

underflow subtracting money amounts

What it means

Runtime panic from the Sub impl for PgMoney (PostgreSQL money type). Subtraction uses i64::checked_sub on the wrapped cents value and panics on underflow in both debug and release builds instead of wrapping, to avoid silently corrupted monetary results. Fires when `a - b` goes below i64::MIN. Fix: avoid subtracting amounts whose difference underflows i64.

Solutions

  1. Validate both amounts before subtracting
  2. Use a wider numeric type for intermediate arithmetic
  3. Clamp or reject results that fall below the money range
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at diesel/src/pg/types/money.rs:73 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of diesel-rs/diesel@6fa6ed01b2 (2026-09-07). Data as JSON: /api/errors/2495b63f8361d73b. Report an issue: GitHub.

Appendix: source

Thrown at diesel/src/pg/types/money.rs:73

    /// Performs a checked addition, and will `panic!` on overflow in both `debug` and `release`.
    fn add_assign(&mut self, rhs: PgMoney) {
        self.0 = self
            .0
            .checked_add(rhs.0)
            .expect("overflow adding money amounts")
    }
}

impl Sub for PgMoney {
    type Output = Self;
    /// # Panics
    ///
    /// Performs a checked subtraction, and will `panic!` on underflow in both `debug` and `release`.
    fn sub(self, rhs: PgMoney) -> Self::Output {
        self.0
            .checked_sub(rhs.0)
            .map(PgMoney)
            .expect("underflow subtracting money amounts")
    }
}

impl SubAssign for PgMoney {
    /// # Panics
    ///
    /// Performs a checked subtraction, and will `panic!` on underflow in both `debug` and `release`.
    fn sub_assign(&mut self, rhs: PgMoney) {
        self.0 = self
            .0
            .checked_sub(rhs.0)
            .expect("underflow subtracting money amounts")
    }
}

#[cfg(feature = "quickcheck")]
mod quickcheck_impls {
    extern crate quickcheck;

View on GitHub (pinned to 6fa6ed01b2)