我正在尝试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::NaiveDate
chono::NaiveDateTime