Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql - the trait `diesel::Expression` is not implemented for `f64`

I'm trying to set up a model using diesel and mysql. My first model compiles without issue but when my second model, the one meant for inserting a new entry to my DB, tries to derive Insertable I get a slew of errors stating that diesel can't use f64, u16, or NaiveDateTime

error[E0277]: the trait bound `u16: diesel::Expression` is not satisfied
  --> src/models.rs:26:10
   |
26 | #[derive(Insertable, Debug, Queryable)]
   |          ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `u16`
   |
   = note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::Unsigned<diesel::sql_types::Integer>>` for `u16`
   = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `chrono::naive::datetime::NaiveDateTime: diesel::Expression` is not satisfied
  --> src/models.rs:26:10
   |
26 | #[derive(Insertable, Debug, Queryable)]
   |          ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `chrono::naive::datetime::NaiveDateTime`
   |
   = note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::Date>` for `chrono::naive::datetime::NaiveDateTime`
   = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `f64: diesel::Expression` is not satisfied
  --> src/models.rs:26:10
   |
26 | #[derive(Insertable, Debug, Queryable)]
   |          ^^^^^^^^^^ 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::Float>` for `f64`
   = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `u16: diesel::Expression` is not satisfied
  --> src/models.rs:26:10
   |
26 | #[derive(Insertable, Debug, Queryable)]
   |          ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `u16`
   |
   = note: required because of the requirements on the impl of `diesel::Expression` for `&u16`
   = note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::Unsigned<diesel::sql_types::Integer>>` for `&u16`
   = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `chrono::naive::datetime::NaiveDateTime: diesel::Expression` is not satisfied
  --> src/models.rs:26:10
   |
26 | #[derive(Insertable, Debug, Queryable)]
   |          ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `chrono::naive::datetime::NaiveDateTime`
   |
   = note: required because of the requirements on the impl of `diesel::Expression` for `&chrono::naive::datetime::NaiveDateTime`
   = note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::Date>` for `&chrono::naive::datetime::NaiveDateTime`
   = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `f64: diesel::Expression` is not satisfied
  --> src/models.rs:26:10
   |
26 | #[derive(Insertable, Debug, Queryable)]
   |          ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `f64`
   |
   = note: required because of the requirements on the impl of `diesel::Expression` for `&f64`
   = note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::Float>` for `&f64`
   = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `u16: diesel::Expression` is not satisfied
  --> src/models.rs:26:10
   |
26 | #[derive(Insertable, Debug, Queryable)]
   |          ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `u16`
   |
   = note: required because of the requirements on the impl of `diesel::Expression` for `&'insert u16`
   = note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::Unsigned<diesel::sql_types::Integer>>` for `&'insert u16`
   = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `chrono::naive::datetime::NaiveDateTime: diesel::Expression` is not satisfied
  --> src/models.rs:26:10
   |
26 | #[derive(Insertable, Debug, Queryable)]
   |          ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `chrono::naive::datetime::NaiveDateTime`
   |
   = note: required because of the requirements on the impl of `diesel::Expression` for `&'insert chrono::naive::datetime::NaiveDateTime`
   = note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::Date>` for `&'insert chrono::naive::datetime::NaiveDateTime`
   = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `f64: diesel::Expression` is not satisfied
  --> src/models.rs:26:10
   |
26 | #[derive(Insertable, Debug, Queryable)]
   |          ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `f64`
   |
   = note: required because of the requirements on the impl of `diesel::Expression` for `&'insert f64`
   = note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::Float>` for `&'insert f64`
   = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

Here's my models.rs file

use crate::schema::*;
use serde::{Deserialize, Serialize};
use chrono::NaiveDateTime;

#[derive(Debug, Serialize, Deserialize, Queryable)]
pub struct Site {
    pub site_id: i32,
    pub name: String,
    pub slug: String,
    pub rack_size: u16,
    pub date_created: chrono::NaiveDateTime,
    pub latitude: f64,
    pub longitude: f64 
}

//#[derive(Insertable, Debug, Queryable)]
//#[table_name = "site"]
//pub struct NewSite<'a> {
//    pub name: &'a str,
//    pub slug: &'a str,
//    pub rack_size: &'a  u16,
//    pub date_created: chrono::NaiveDateTime,
//    pub latitude: &'a f64,
//    pub longitude: &'a f64
//}
#[derive(Insertable, Debug, Queryable)]
#[table_name = "site"]
pub struct NewSite {
    pub name: String,
    pub slug: String,
    pub rack_size: u16,
    pub date_created: chrono::NaiveDateTime,
    pub latitude: f64,
    pub longitude: f64 
}

And my Cargo.toml

[package]
name = "micd"
version = "0.1.0"
authors = ["First Last <[email protected]>"]
edition = "2018"

[dependencies]
actix-web = "2.0.0"
actix-web-httpauth = { git = "https://github.com/actix/actix-web-httpauth" }
chrono = { version = "0.4.10", features = ["serde"] }
derive_more = "0.99.2"
diesel = { version = "1.4.2", features = ["extras", "mysql", "default"] }
dotenv = "0.15.0"
futures = "0.3.1"
r2d2 = "0.8.8"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
actix-service = "1.0.1"
alcoholic_jwt = "1.0.0"
reqwest = "0.9.22"
actix-rt = "1.0.0"

I've tried importing the numeric feature in diesel as suggested by the only SO question I could find that relates to this. All guides I've looked at just walk right past this issue and I can't see what I might be doing wrong.

like image 235
twiclo Avatar asked Sep 01 '20 22:09

twiclo


1 Answers

Turns out there are mappings of which sql type should be used with which rust type and vice versa. The mappings can be found either here: https://docs.rs/diesel/1.4.5/diesel/deserialize/trait.FromSql.html#impl-FromSql%3CDatetime%2C%20Mysql%3E-for-MYSQL_TIME

or here: https://docs.rs/diesel/1.4.5/diesel/sql_types/index.html

like image 151
twiclo Avatar answered Nov 04 '22 06:11

twiclo