mysql-特质`diesel :: Expression`未针对`f64`实现

问题描述

我正在尝试使用柴油和MysqL建立模型。我的第一个模型可以毫无问题地编译,但是当我的第二个模型(用于向数据库中插入新条目的模型)试图派生出selectStyles时,我会遇到很多错误,指出柴油机无法使用Insertablef64u16

NaiveDateTime

这是我的models.rs文件

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,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,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,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,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,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,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,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,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)

还有我的Cargo.toml

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,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,Queryable)]
#[table_name = "site"]
pub struct NewSite {
    pub name: String,pub longitude: f64 
}

我尝试按照the only SO question I could find that relates to this的建议将数字功能导入柴油。我看过的所有指南都绕过了这个问题,我看不出我做错了什么。

解决方法

结果是,存在应将哪种sql类型与哪种rust类型一起使用的映射,反之亦然。可以在以下位置找到映射: https://docs.rs/diesel/1.4.5/diesel/deserialize/trait.FromSql.html#impl-FromSql%3CDatetime%2C%20Mysql%3E-for-MYSQL_TIME

或此处: https://docs.rs/diesel/1.4.5/diesel/sql_types/index.html