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

Can not use .eq(Decimal_field + xxx) #3961

Open
3 tasks done
kakalos12 opened this issue Mar 11, 2024 · 4 comments
Open
3 tasks done

Can not use .eq(Decimal_field + xxx) #3961

kakalos12 opened this issue Mar 11, 2024 · 4 comments

Comments

@kakalos12
Copy link

kakalos12 commented Mar 11, 2024

Hi,
Really love diesel, but I got some trouble with the diesel Decimal. Please help.

Setup

Versions

  • Rust: latest
  • Diesel: 2.1.4
  • **Database: mysql **
  • **Operating System : Macos **

Feature Flags

  • diesel: numeric, mysql

Problem Description

use std::borrow::Cow;

use bigdecimal::BigDecimal;
use diesel::prelude::*;
diesel::table! {
    customer_wallets (id) {
        id -> Unsigned<Integer>,
        #[max_length = 36]
        customer_uuid -> Char,
        balance -> Decimal,
        credits -> Decimal,
    }
}

#[derive(Queryable, Selectable)]
#[diesel(table_name = customer_wallets)]
#[diesel(check_for_backend(diesel::mysql::Mysql))]
pub struct CustomerWallet<'a> {
    pub id: u32,
    pub customer_uuid: Cow<'a, str>,
    pub balance: BigDecimal,
    pub credits: BigDecimal,
}
fn main() {
    let a = BigDecimal::from_u32(3).unwrap();
    diesel::update(customer_wallets::table)
        .set(customer_wallets::balance.eq(customer_wallets::balance + a));
    println!("Hello, world!");
}


What are you trying to accomplish?

What is the expected output?

What is the actual output?

Are you seeing any additional errors?

   Compiling app v0.1.0 (/Users/dongnguyen/Projects/learning/app)
error[E0369]: cannot add `BigDecimal` to `columns::balance`
  --> src/main.rs:27:69
   |
27 |         .set(customer_wallets::balance.eq(customer_wallets::balance + a));
   |                                           ------------------------- ^ - BigDecimal
   |                                           |
   |                                           columns::balance
   |
note: an implementation of `std::ops::Add<BigDecimal>` might be missing for `columns::balance`
  --> src/main.rs:10:9
   |
10 |         balance -> Decimal,
   |         ^^^^^^^ must implement `std::ops::Add<BigDecimal>`
note: the trait `std::ops::Add` must be implemented
  --> /Users/dongnguyen/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/arith.rs:76:1
   |
76 | pub trait Add<Rhs = Self> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0369`.
error: could not compile `app` (bin "app" test) due to 1 previous error

Steps to reproduce

Checklist

  • This issue can be reproduced on Rust's stable channel. (Your issue will be
    closed if this is not the case)
  • This issue can be reproduced without requiring a third party crate
@kakalos12 kakalos12 added the bug label Mar 11, 2024
@weiznich
Copy link
Member

Thanks for opening this bug report.

Seems like it's just missing the BigDecimal type in this list of allowed types:

const NUMERIC_TYPES: &[&str] = &[

Mind submitting a PR that adds it there + adds a test somewhere in diesel_test

@kakalos12
Copy link
Author

ok @weiznich , Let me try it.

@kakalos12
Copy link
Author

Hi @weiznich , Sorry, I'm new to rust, Can you guide me how to setup the working env for the diesel. When I clone the repository to my local machine and try to run cargo t, it has some errors

error[E0432]: unresolved import `diesel`
  --> diesel/src/expression/functions/date_and_time.rs:30:1
   |
30 | / define_sql_function! {
31 | |     /// Represents the SQL `DATE` function. The argument should be a Timestamp
32 | |     /// expression, and the return value will be an expression of type Date.
33 | |     ///
...  |
50 | |     fn date(expr: Timestamp) -> Date;
51 | | }
   | |_^ could not find `sqlite` in the crate root
   |
   = note: this error originates in the macro `define_sql_function` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0432]: unresolved import `diesel`
  --> diesel/src/sql_types/mod.rs:40:48
   |
40 | #[derive(Debug, Clone, Copy, Default, QueryId, SqlType)]
   |                                                ^^^^^^^ could not find `pg` in the crate root
   |
   = note: this error originates in the derive macro `SqlType` (in Nightly builds, run with -Z macro-backtrace for more info)

Thanks so much.

@weiznich
Copy link
Member

It depends a bit on where exactly you've called cargo test, but in general you always need to set some feature flags to enable one or the other backend. The contribution documentation has some more details and there is the bin/test script which executes most of the tests in the repo.

For the diesel crate itself you usually want something like DATABASE_URL=… cargo test --features "{backend_to_test} extras" where {backend_to_test} is one of postgres, mysql or sqlite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants