Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[postgres] Problem with bigdecimal #283

Closed
funtoy opened this issue Apr 27, 2020 · 2 comments · Fixed by #284
Closed

[postgres] Problem with bigdecimal #283

funtoy opened this issue Apr 27, 2020 · 2 comments · Fixed by #284
Labels
bug db:postgres Related to PostgreSQL

Comments

@funtoy
Copy link

funtoy commented Apr 27, 2020

my wallet_balance default value(numeric) is 0 in my database, and the value is 0 too.
here is my code

sqlx = { version = "0.3.4", features = [ "postgres", "bigdecimal" ] }

use anyhow::Result;
use sqlx::postgres::PgPool;
use sqlx::{PgConnection, Pool};
use sqlx::prelude::*;
use sqlx::types::BigDecimal;

#[derive(Debug, sqlx::FromRow, sqlx::Type)]
pub struct ClientMember {
    pub id: i32,
    pub wallet_balance: BigDecimal,
}

#[actix_rt::main]
async fn main() -> Result<(), sqlx::Error> {
    let url = "postgres://postgres:postgres@localhost/my_db";
    let pool = PgPool::builder()
        .max_size(5)
        .build(url).await?;

    let c = get_columns(pool).await?;
    println!("{:?}", c);

    Ok(())
}

async fn get_columns(pool: Pool<PgConnection>) -> Result<ClientMember, sqlx::Error> {
    let rec = sqlx::query_as(r#"SELECT id,wallet_balance FROM my_table where id=$1"#)
        .bind(655929_i32)
        .fetch_one(&pool).await?;

    Ok(rec)
}

it panic when i run the code,
message:

thread 'main' panicked at 'assertion failed: !v.is_empty() && !radix.is_power_of_two()', /Users/xxx/.cargo/registry/src/github.com-1ecc6299db9ec823/num-bigint-0.2.6/src/biguint.rs:199:5
stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at /Users/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.44/src/backtrace/libunwind.rs:86
   1: backtrace::backtrace::trace_unsynchronized
             at /Users/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.44/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print_fmt
             at src/libstd/sys_common/backtrace.rs:78
   3: <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt
             at src/libstd/sys_common/backtrace.rs:59
...

But when i update my wallet_balance to 0.1, it works! Did i miss the derive or something else?
i am new to rust.
thanks.

@abonander
Copy link
Collaborator

This sounds like a bug to me, I'll look into it.

@abonander abonander added bug db:postgres Related to PostgreSQL labels Apr 27, 2020
@abonander
Copy link
Collaborator

Yep, looks like Postgres sends an empty value array for 0 but we're not handling that properly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug db:postgres Related to PostgreSQL
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants