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

Queryable Numeric is not implemented for BigDecimal #1753

Closed
fundon opened this Issue Jun 5, 2018 · 4 comments

Comments

Projects
None yet
3 participants
@fundon

fundon commented Jun 5, 2018

Setup

Versions

  • Rust: 1.26.1
  • Diesel: 1.3.1
  • Database: PostgreSQL 10
  • Operating System macOS 10.13.5

Feature Flags

  • diesel:

diesel = { git = "https://github.com/diesel-rs/diesel", tag ="v1.3.1", default-features = false, features = ["chrono", "serde_json", "uuid", "network-address", "numeric", "r2d2", "postgres"] }

Problem Description

let results = contracts.limit(5).load::<Contract>(&connection).expect(
   |                                      ^^^^ the trait `diesel::Queryable<diesel::sql_types::Numeric, diesel::pg::Pg>` is not implemented for `bigdecimal::BigDecimal`

Codes:
main.rs

#[macro_use]
extern crate diesel;
extern crate bigdecimal;
extern crate chrono;
extern crate dotenv;
extern crate serde;
#[macro_use]
extern crate serde_json;
#[macro_use]
extern crate serde_derive;

use diesel::pg::PgConnection;
use diesel::prelude::*;
use dotenv::dotenv;
use std::env;

mod schema;

use bigdecimal::BigDecimal;
use chrono::{DateTime, Utc};
use diesel::prelude::*;
use diesel::*;
use serde_json::Value;

#[derive(Queryable)]
pub struct Contract {
    pub id: i32,
    pub conditions: Value,
    pub state: i16,
    pub self_stake: BigDecimal,
    pub rival_stake: BigDecimal,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

pub fn establish_connection() -> PgConnection {
    dotenv().ok();

    let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
    PgConnection::establish(&database_url).expect(&format!("Error connecting to {}", database_url))
}

fn main() {
    let connection = establish_connection();

    use self::diesel::prelude::*;
    use self::schema::contracts::dsl::*;

    let results = contracts
        .limit(5)
        .load::<Contract>(&connection)
        .expect("Error loading posts");

    println!("Displaying {} posts", results.len());
    for post in results {
        println!("{}", post.id);
        println!("----------\n");
        println!("{}", post.conditions);
        println!("{}", post.self_stake);
        println!("{}", post.rival_stake);
    }
}

schema.rs

table! {
    contracts (id) {
        id -> Int4,
        conditions -> Json,
        state -> Int2,
        self_stake -> Numeric,
        rival_stake -> Numeric,
        created_at -> Timestamptz,
        updated_at -> Timestamptz,
    }
}

NOTE: I'm noob.

@weiznich

This comment has been minimized.

Contributor

weiznich commented Jun 5, 2018

That sounds like you are using different versions of bigdecimal. Please doublecheck that your crate depends on the same version as diesel. (Cargo tree could be handy for this)

@weiznich

This comment has been minimized.

Contributor

weiznich commented Jun 5, 2018

Closed because this does not contain something actionable for the diesel team.
(If this does not solve the issue, just continue asking here or even better in our gitter room)

@weiznich weiznich closed this Jun 5, 2018

@fundon

This comment has been minimized.

fundon commented Jun 5, 2018

thank you. fixed

@derekdreery

This comment has been minimized.

Contributor

derekdreery commented Sep 7, 2018

It would be good if diesel re-exported BigDecimal when the numeric feature is enabled, then there's no chance of a clash.

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