How to convert i32 & f32 into Bigdecimal?

Rust

let amount: i32 = 20;
let amount_daily: f32 = 4.2;

add.sql

amount                  INT                NOT NULL,
amount_daily            REAL               NOT NULL,

SQLX
query_file!("add.sql", amount, amount_daily)

Cargo.toml

sqlx = { version = "^0.7.4", features = [
    "runtime-tokio-native-tls",
    "postgres",
    "macros",
    "chrono",
    "bigdecimal",
] }
bigdecimal = { version = "^0.4.3", features = ["serde"] }

Error

the trait bound `bigdecimal::BigDecimal: Encode<'_, Postgres>` is not satisfied
the following other types implement trait `Encode<'q, DB>`:
  <bool as Encode<'q, sqlx::Any>>
  <bool as Encode<'_, Postgres>>
  <i8 as Encode<'_, Postgres>>
  <i16 as Encode<'q, sqlx::Any>>
  <i16 as Encode<'_, Postgres>>
  <i32 as Encode<'q, sqlx::Any>>
  <i32 as Encode<'_, Postgres>>
  <i64 as Encode<'q, sqlx::Any>>
and 41 others
required for `&bigdecimal::BigDecimal` to implement `Encode<'_, Postgres>`rustcClick for full compiler diagnostic
mod.rs(398, 9): Actual error occurred here
mod.rs(398, 9): Error originated from macro call here
the trait bound `bigdecimal::BigDecimal: sqlx::Type<Postgres>` is not satisfied
the following other types implement trait `sqlx::Type<DB>`:
  <bool as sqlx::Type<Postgres>>
  <bool as sqlx::Type<sqlx::Any>>
  <i8 as sqlx::Type<Postgres>>
  <i16 as sqlx::Type<Postgres>>
  <i16 as sqlx::Type<sqlx::Any>>
  <i32 as sqlx::Type<Postgres>>
  <i32 as sqlx::Type<sqlx::Any>>
  <i64 as sqlx::Type<Postgres>>
and 52 others
required for `&bigdecimal::BigDecimal` to implement `sqlx::Type<Postgres>`
1 redundant requirement hidden
required for `&&bigdecimal::BigDecimal` to implement `sqlx::Type<Postgres>`rustcClick for full compiler diagnostic
mod.rs(398, 9): Actual error occurred here
mod.rs(398, 9): Error originated from macro call here
arguments.rs(19, 53): required by a bound in `sqlx::Arguments::add`

Please help
Thanks

Note that sqlx depends on bigdecimal 0.3, whereas you import the incompatible bigdecimal 0.4, which could be the reason for the error message that the trait isn't implemented for bigdecimal::BigDecimal (as the BigDecimal from bigdecimal 0.4 is a distinct type from BigDecimal from bigdecimal 0.3, which should implement Encode).

Thanks
I changed the version to 0.3 & now getting different error.

the trait bound `BigDecimal: From<f32>` is not satisfied
the following other types implement trait `From<T>`:
  <BigDecimal as From<i8>>
  <BigDecimal as From<i16>>
  <BigDecimal as From<i32>>
  <BigDecimal as From<i64>>
  <BigDecimal as From<u8>>
  <BigDecimal as From<u16>>
  <BigDecimal as From<u32>>
  <BigDecimal as From<u64>>
and 2 othersrustcClick for full compiler diagnostic
bigdecimal
// size = 40 (0x28), align = 0x8
pub struct BigDecimal
A big decimal type.

Why sqlx does not re-export the bigdecimal which they are using in their package.

It does.

1 Like

The column data type is REAL.
Still sqlx asking for Bigdecimal.
So what I did is Bigdecimal::from(amount_daily) but even without convert & just amount_daily both are not working.

Error

expected `BigDecimal`, found `f32`

So strange, BigDecimal should only be used for NUMERIC columns, according to the docs.

Yes that is the issue.
Its asking for REAL column too.

What if you convert your f32 to BigDecimal with TryFrom instead?

I did Bigdecimal::from(amount_daily) but even without convert & just amount_daily both are not working.

Also sqlx not re-exporting all the Bigdecimal thing or I can not find it.
This ToPrimitive

You should be able to import it from ::sqlx::types::BigDecimal.

Could you try BigDecimal::try_from(amount_daily).unwrap() instead?

1 Like

With Bigdecimal::try_from(amount_daily) I am getting this same error

Error

the trait bound `Result<BigDecimal, Infallible>: Encode<'_, Postgres>` is not satisfied
the following other types implement trait `Encode<'q, DB>`:
  <bool as Encode<'q, sqlx::Any>>
  <bool as Encode<'_, Postgres>>
  <i8 as Encode<'_, Postgres>>
  <i16 as Encode<'q, sqlx::Any>>
  <i16 as Encode<'_, Postgres>>
  <i32 as Encode<'q, sqlx::Any>>
  <i32 as Encode<'_, Postgres>>
  <i64 as Encode<'q, sqlx::Any>>
and 41 othersrustcClick for full compiler diagnostic
mod.rs(398, 9): Actual error occurred here
mod.rs(398, 9): Error originated from macro call here
the trait bound `Result<BigDecimal, bigdecimal::ParseBigDecimalError>: Encode<'_, Postgres>` is not satisfied
the following other types implement trait `Encode<'q, DB>`:
  <bool as Encode<'q, sqlx::Any>>
  <bool as Encode<'_, Postgres>>
  <i8 as Encode<'_, Postgres>>
  <i16 as Encode<'q, sqlx::Any>>
  <i16 as Encode<'_, Postgres>>
  <i32 as Encode<'q, sqlx::Any>>
  <i32 as Encode<'_, Postgres>>
  <i64 as Encode<'q, sqlx::Any>>
and 41 othersrustcClick for full compiler diagnostic
mod.rs(398, 9): Actual error occurred here
mod.rs(398, 9): Error originated from macro call here
the trait bound `Result<BigDecimal, Infallible>: sqlx::Type<Postgres>` is not satisfied
the following other types implement trait `sqlx::Type<DB>`:
  <bool as sqlx::Type<Postgres>>
  <bool as sqlx::Type<sqlx::Any>>
  <i8 as sqlx::Type<Postgres>>
  <i16 as sqlx::Type<Postgres>>
  <i16 as sqlx::Type<sqlx::Any>>
  <i32 as sqlx::Type<Postgres>>
  <i32 as sqlx::Type<sqlx::Any>>
  <i64 as sqlx::Type<Postgres>>
and 52 others
required for `&Result<BigDecimal, Infallible>` to implement `sqlx::Type<Postgres>`rustcClick for full compiler diagnostic
mod.rs(398, 9): Actual error occurred here
mod.rs(398, 9): Error originated from macro call here
arguments.rs(19, 53): required by a bound in `sqlx::Arguments::add`
the trait bound `Result<BigDecimal, bigdecimal::ParseBigDecimalError>: sqlx::Type<Postgres>` is not satisfied
the following other types implement trait `sqlx::Type<DB>`:
  <bool as sqlx::Type<Postgres>>
  <bool as sqlx::Type<sqlx::Any>>
  <i8 as sqlx::Type<Postgres>>
  <i16 as sqlx::Type<Postgres>>
  <i16 as sqlx::Type<sqlx::Any>>
  <i32 as sqlx::Type<Postgres>>
  <i32 as sqlx::Type<sqlx::Any>>
  <i64 as sqlx::Type<Postgres>>
and 52 others
required for `&Result<BigDecimal, bigdecimal::ParseBigDecimalError>` to implement `sqlx::Type<Postgres>`rustcClick for full compiler diagnostic
mod.rs(398, 9): Actual error occurred here
mod.rs(398, 9): Error originated from macro call here
arguments.rs(19, 53): required by a bound in `sqlx::Arguments::add`

sqlx::types::BigDecimal
This one found
But ToPrimitive can not found

You need to unwrap the result or otherwise handle the Result. BigDecimal::try_from(amount_daily).unwrap().

1 Like

ToPrimitive is not re-exported so getting different error.

no method named `to_f32` found for struct `BigDecimal` in the current scope

ToPrimitive is defined in num-traits 0.2.

Yes & that is not re-exported from sqlx.
Now num-traitsmight have different version.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.