英文:
the trait `FromSql<diesel::sql_types::Timestamptz, Pg>` is not implemented for `DateTime<Utc>`
问题
我尝试编译我的Rust Diesel项目时,尽管diesel文档显示实现了此特性,但出现以下错误:
未满足特性绑定
DateTime<Utc>: FromSql<diesel::sql_types::Timestamptz, Pg>
我的schema.rs如下:
pub mod offers {
diesel::table! {
offers.offers (id) {
id -> Int4,
#[max_length = 255]
offername -> Varchar,
#[max_length = 255]
offertypeid -> Nullable<Varchar>,
startdate -> Nullable<Timestamptz>,
enddate -> Nullable<Timestamptz>,
frequency -> Nullable<Int4>,
#[max_length = 255]
createdby -> Nullable<Varchar>,
createdAt -> Nullable<Timestamptz>
}
}
}
这是我的models.rs:
use diesel::prelude::*;
use chrono::{DateTime, Utc};
use crate::schema::offers::offers as offerTable;
#[derive(Queryable, Selectable)]
#[diesel(table_name = offerTable)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct Offer {
pub id: i32,
pub enddate: Option<DateTime<Utc>>,
pub createdAt: Option<DateTime<Utc>>,
pub createdby: Option<String>,
pub frequency: Option<i32>,
pub offername: String,
pub startdate: Option<DateTime<Utc>> //<--- 此行出错
}
在Diesel文档中,它显示此映射应该有效,但我无法弄清楚为什么它不起作用。
英文:
I'm getting the following error when trying to compile my rust diesel project despite the diesel docs showing that this trait is implemented
> the trait bound DateTime<Utc>: FromSql<diesel::sql_types::Timestamptz, Pg>
is not satisfied
My schema.rs
pub mod offers {
diesel::table! {
offers.offers (id) {
id -> Int4,
#[max_length = 255]
offername -> Varchar,
#[max_length = 255]
offertypeid -> Nullable<Varchar>,
startdate -> Nullable<Timestamptz>,
enddate -> Nullable<Timestamptz>,
frequency -> Nullable<Int4>,
#[max_length = 255]
createdby -> Nullable<Varchar>,
createdAt -> Nullable<Timestamptz>
}
}
}
Here is my models.rs:
use diesel::prelude::*;
use chrono::{DateTime, Utc};
use crate::schema::offers::offers as offerTable;
#[derive(Queryable, Selectable)]
#[diesel(table_name = offerTable)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct Offer {
pub id: i32,
pub enddate: Option<DateTime<Utc>>,
pub createdAt: Option<DateTime<Utc>>,
pub createdby: Option<String>,
pub frequency: Option<i32>,
pub offername: String,
pub startdate: Option<DateTime<Utc>> <--- ERROR FOR THIS LINE
}
In the docs for diesel here is shows that this mapping should work and I haven't been able to figure out why it's not working
答案1
得分: 1
你需要为 DateTime<UTC>
的实现启用 "chrono"
特性,以便使用 chrono crate 提供的功能。这在文档中作为注释显示,不会默认启用。你可以在 Diesel 的 crate feature flags 部分中了解更多关于此特性和其他特性的信息。
因此,你的 Cargo.toml
至少应包含以下内容:
[dependencies]
diesel = { version = "2.1.0", features = ["postgres", "chrono"] }
Diesel 版本 1.x 也适用这个设置。
英文:
You need to enable the "chrono"
feature for the implementation for DateTime<UTC>
from the chrono crate to be provided. This is shown as an annotation in the docs and is not enabled by default. You can read more about this feature and others in Diesel's crate feature flags section of the docs.
So your Cargo.toml
should contain at least this:
[dependencies]
diesel = { version = "2.1.0", features = ["postgres", "chrono"] }
This goes for Diesel version 1.x as well.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论