Traitdiesel::Expression 未针对 NaiveDate 实现,但适用于 NaiveDateTime

ted*_*ner -1 rust rust-cargo rust-chrono rust-diesel

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

\n
use chrono::{NaiveDate, NaiveDateTime};\nuse diesel::{Insertable, Queryable};\nuse serde::{Deserialize, Serialize};\n\nuse crate::schema::users;\n\n#[derive(Debug, Serialize, Deserialize, Associations, Identifiable, Queryable)]\n#[table_name = "users"]\npub struct User {\n    pub id: uuid::Uuid,\n    pub password_hash: String,\n    pub is_active: bool,\n\n    pub is_premium: bool,\n    pub premium_expiration: Option<NaiveDate>,\n\n    pub email: String,\n    pub first_name: String,\n    pub last_name: String,\n    pub date_of_birth: NaiveDate,\n    pub currency: String,\n\n    pub modified_timestamp: NaiveDateTime,\n    pub created_timestamp: NaiveDateTime,\n}\n\n#[derive(Debug, Insertable)]\n#[table_name = "users"]\npub struct NewUser<'a> {\n    pub id: uuid::Uuid,\n    pub password_hash: &'a str,\n    pub is_active: bool,\n\n    pub is_premium: bool,\n    pub premium_expiration: Option<NaiveDate>,\n\n    pub email: &'a str,\n    pub first_name: &'a str,\n    pub last_name: &'a str,\n    pub date_of_birth: NaiveDate,\n    pub currency: &'a str,\n\n    pub modified_timestamp: NaiveDateTime,\n    pub created_timestamp: NaiveDateTime,\n}\n
Run Code Online (Sandbox Code Playgroud)\n

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

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

我的相关行Cargo.toml

\n
[dependencies]\nchrono = { version = "0.4", features = ["serde"] }\ndiesel = { version = "1.4", features = ["postgres", "uuidv07", "r2d2", "chrono"] }\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 chrono v0.4.19\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 chrono v0.4.19 (*)\n\xe2\x94\x82       \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 chrono v0.4.19 (*)\n
Run Code Online (Sandbox Code Playgroud)\n

NaiveDate以前曾在柴油模型中使用过,并且在导出宏时没有任何问题Insertable。我在这里缺少什么阻止宏实现diesel::Expressionforchono::NaiveDate而它似乎已实现chono::NaiveDateTime

\n

wei*_*ich 5

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

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

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