特征 diesel::Expression 未为 NaiveDate 实现,但它适用于 NaiveDateTime

Trait diesel::Expression not implemented for NaiveDate, but it is for NaiveDateTime

我正在尝试使用 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 实现的?

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

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

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