【发布时间】: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