【问题标题】:mysql - the trait `diesel::Expression` is not implemented for `f64`mysql - 特性 `diesel::Expression` 没有为`f64` 实现
【发布时间】:2020-12-21 01:53:14
【问题描述】:

我正在尝试使用柴油和 mysql 建立模型。我的第一个模型编译没有问题,但是当我的第二个模型(用于向我的数据库插入新条目的模型)尝试派生 Insertable 时,我收到大量错误,指出柴油不能使用 f64u16 ,或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)

这是我的 models.rs 文件

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 
}

还有我的 Cargo.toml

[package]
name = "micd"
version = "0.1.0"
authors = ["First Last <no@gmail.com>"]
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"

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

【问题讨论】:

    标签: mysql rust rust-diesel


    【解决方案1】:

    原来存在应该使用哪种 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

    【讨论】:

      猜你喜欢
      • 2019-09-10
      • 2023-02-07
      • 2022-01-22
      • 2023-04-04
      • 2021-04-25
      • 2021-12-05
      • 2019-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多