【问题标题】:Trait diesel::Expression not implemented for NaiveDate, but it is for NaiveDateTime特性diesel::Expression 未为NaiveDate 实现,但它适用于NaiveDateTime
【发布时间】:2022-01-22 13:47:03
【问题描述】:

我正在尝试使用chrono::NaiveDate 作为数据库模型字段。这是模型:

use chrono::{NaiveDate, NaiveDateTime};
use diesel::{Insertable, Queryable};
use serde::{Deserialize, Serialize};

use crate::schema::users;

#[derive(Debug, Serialize, Deserialize, Associations, Identifiable, Queryable)]
#[table_name = "users"]
pub struct User {
    pub id: uuid::Uuid,
    pub password_hash: String,
    pub is_active: bool,

    pub is_premium: bool,
    pub premium_expiration: Option<NaiveDate>,

    pub email: String,
    pub first_name: String,
    pub last_name: String,
    pub date_of_birth: NaiveDate,
    pub currency: String,

    pub modified_timestamp: NaiveDateTime,
    pub created_timestamp: NaiveDateTime,
}

#[derive(Debug, Insertable)]
#[table_name = "users"]
pub struct NewUser<'a> {
    pub id: uuid::Uuid,
    pub password_hash: &'a str,
    pub is_active: bool,

    pub is_premium: bool,
    pub premium_expiration: Option<NaiveDate>,

    pub email: &'a str,
    pub first_name: &'a str,
    pub last_name: &'a str,
    pub date_of_birth: NaiveDate,
    pub currency: &'a str,

    pub modified_timestamp: NaiveDateTime,
    pub created_timestamp: NaiveDateTime,
}

当我运行 cargo check 时,我从 rustc 收到以下错误:

error[E0277]: the trait bound `NaiveDate: diesel::Expression` is not satisfied
  --> src/models/user.rs:27:17
   |
27 | #[derive(Debug, Insertable)]
   |                 ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `NaiveDate`
   |
   = note: required because of the requirements on the impl of `AsExpression<diesel::sql_types::Nullable<diesel::sql_types::Timestamp>>` for `NaiveDate`
   = note: this error originates in the derive macro `Insertable` (in Nightly builds, run with -Z macro-backtrace for more info)

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

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

For more information about this error, try `rustc --explain E0277`.

Cargo.toml的相关行:

[dependencies]
chrono = { version = "0.4", features = ["serde"] }
diesel = { version = "1.4", features = ["postgres", "uuidv07", "r2d2", "chrono"] }

运行cargo tree | grep chrono给出如下输出,说明与chrono不存在版本冲突:

├── chrono v0.4.19
│   ├── chrono v0.4.19 (*)
│       ├── chrono v0.4.19 (*)

我之前在柴油车型中使用过NaiveDate,并且在导出Insertable 宏时没有遇到任何问题。我在这里缺少什么阻止宏为chono::NaiveDate 实现diesel::Expression 而它似乎是为chono::NaiveDateTime 实现的?

【问题讨论】:

    标签: rust rust-cargo rust-diesel rust-chrono


    【解决方案1】:

    首先,您的问题缺少重要信息。这包括来自您的架构的相应 table! 调用。

    现在根据您提供的信息猜测可能导致此问题的原因:

    错误消息表明您尝试将NaiveDate 插入Timestamp 字段。这只是不支持的东西,因为时间戳需要数据和时间值,而 NaiveDate 仅提供日期值。您可以在每种 SQL 类型的文档中查看现成支持的柴油类型映射列表。例如Timestamphere

    【讨论】:

    • 愚蠢的我忘记将模式添加到问题描述中。谢谢;你是对的,我在 SQL 中将其设为 TIMESTAMP 字段。
    猜你喜欢
    • 2020-12-21
    • 2019-09-10
    • 2023-02-07
    • 2021-03-30
    • 2023-04-04
    • 2021-04-25
    • 2022-08-03
    • 1970-01-01
    • 2021-06-07
    相关资源
    最近更新 更多