diesel-rs/diesel · error

overflow adding money amounts

Error message

overflow adding money amounts

What it means

Runtime panic from the Add impl for PgMoney (PostgreSQL money type). PgMoney wraps an i64 cents value; addition uses i64::checked_add and deliberately panics on overflow in both debug and release builds rather than wrapping around, because a wrapped monetary amount would be silently wrong. Fires when `a + b` exceeds i64::MAX. Fix: use smaller amounts or handle the sum with checked arithmetic before adding.

Solutions

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

Strategy: validation

When it happens

Trigger: Thrown at diesel/src/pg/types/money.rs:48 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/ea0e5716f3af3d0a. Report an issue: GitHub.

Appendix: source

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

}

#[cfg(feature = "postgres_backend")]
impl ToSql<Money, Pg> for PgMoney {
    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
        ToSql::<BigInt, Pg>::to_sql(&self.0, out)
    }
}

impl Add for PgMoney {
    type Output = Self;
    /// # Panics
    ///
    /// Performs a checked addition, and will `panic!` on overflow in both `debug` and `release`.
    fn add(self, rhs: PgMoney) -> Self::Output {
        self.0
            .checked_add(rhs.0)
            .map(PgMoney)
            .expect("overflow adding money amounts")
    }
}

impl AddAssign for PgMoney {
    /// # Panics
    ///
    /// 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

View on GitHub (pinned to 6fa6ed01b2)