-1

我正在尝试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我在这里遗漏了什么阻止宏在它似乎被实现的情况下diesel::Expression实现?chono::NaiveDatechono::NaiveDateTime

4

1 回答 1

1

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

现在根据您提供的信息猜测可能导致这种情况的原因:

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

于 2021-12-21T08:34:38.437 回答