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.
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.