3

I started with diesel and rocket in Rust and have a problem with inserting floating values to the database. My struct looks like:

#[derive(Serialize, Deserialize, Insertable)]
#[table_name = "database"]
pub struct New_Data{
    pub data1: f64,
    pub data2: f64,
    pub data3: f64,
}

and I get this error: the trait bound f64: diesel::Expression is not satisfied label: the trait diesel::Expression is not implemented for f64, note: required because of the requirements on the impl of diesel::expression::AsExpression<diesel::sql_types::Numeric> for f64

I've read that diesel kinda uses its own data/SQL types but I have no idea how to declare a Float. I also tried to use diesel::sql_types::Float with a similar error message.

Jojo
  • 70
  • 6
  • Does this answer your question? [the trait \`diesel::Expression\` is not implemented for \`bigdecimal::BigDecimal\`](https://stackoverflow.com/questions/55783064/the-trait-dieselexpression-is-not-implemented-for-bigdecimalbigdecimal) – Stargateur Apr 04 '20 at 17:51
  • Just realized my mistake was in the database (schema.rs). Just had the whole time the wrong type in my database and didn't notice. – Jojo Apr 04 '20 at 18:41

1 Answers1

4

This looks like a mismatch between the schema type of the field, as defined in the diesel auto-generated schema.rs, and the type of the field you defined in New_Data. Look inside the auto-generated schema for a data1 -> definition, you may find something like:

data1 -> Float4

In this case the type of the field needs to be f32. Otherwise, if it's Float8, then the type should be f64. This mapping between the language of diesel schema types extends further to Option and Nullable. If it appears as Nullable<Float4> in the schema, then it should be Option<f32> in the type.

Dan Aloni
  • 3,968
  • 22
  • 30
  • Ah got it. My mistake was in the migration up.sql. I put the wrong type in there. Thanks for the hint – Jojo Apr 04 '20 at 18:40
  • @Jojo - Could you share your CREATE TABLE sql with the float columns? I have tried various attempts with `FLOAT`, `DOUBLE` or even `DECIMAL`, but all fail. – grizzthedj Feb 06 '22 at 18:34