Diesel: Queryable Numeric is not implemented for BigDecimal

Created on 5 Jun 2018  路  5Comments  路  Source: diesel-rs/diesel

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.

Most helpful comment

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

All 5 comments

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)

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)

thank you. fixed

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

thanks a lot

Was this page helpful?
0 / 5 - 0 ratings