“FromSql“这个特性未实现对于”DateTime“。

huangapple go评论65阅读模式
英文:

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文档中,它显示此映射应该有效,但我无法弄清楚为什么它不起作用。
“FromSql<diesel::sql_types::Timestamptz, Pg>“这个特性未实现对于”DateTime<Utc>“。

英文:

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&lt;Utc&gt;: FromSql&lt;diesel::sql_types::Timestamptz, Pg&gt; is not satisfied

My schema.rs

pub mod offers {
    diesel::table! {
        offers.offers (id) {
            id -&gt; Int4,
            #[max_length = 255]
            offername -&gt; Varchar,
            #[max_length = 255]
            offertypeid -&gt; Nullable&lt;Varchar&gt;,
            startdate -&gt; Nullable&lt;Timestamptz&gt;,
            enddate -&gt; Nullable&lt;Timestamptz&gt;,
            frequency -&gt; Nullable&lt;Int4&gt;,
            #[max_length = 255]
            createdby -&gt; Nullable&lt;Varchar&gt;,
            createdAt -&gt; Nullable&lt;Timestamptz&gt;
        }
    }
}

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&lt;DateTime&lt;Utc&gt;&gt;,
    pub createdAt: Option&lt;DateTime&lt;Utc&gt;&gt;,
    pub createdby: Option&lt;String&gt;,
    pub frequency: Option&lt;i32&gt;,
    pub offername: String,
    pub startdate: Option&lt;DateTime&lt;Utc&gt;&gt; &lt;--- 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
“FromSql<diesel::sql_types::Timestamptz, Pg>“这个特性未实现对于”DateTime<Utc>“。

答案1

得分: 1

你需要为 DateTime<UTC> 的实现启用 &quot;chrono&quot; 特性,以便使用 chrono crate 提供的功能。这在文档中作为注释显示,不会默认启用。你可以在 Diesel 的 crate feature flags 部分中了解更多关于此特性和其他特性的信息。

因此,你的 Cargo.toml 至少应包含以下内容:

[dependencies]
diesel = { version = &quot;2.1.0&quot;, features = [&quot;postgres&quot;, &quot;chrono&quot;] }

Diesel 版本 1.x 也适用这个设置。

英文:

You need to enable the &quot;chrono&quot; feature for the implementation for DateTime&lt;UTC&gt; 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 = &quot;2.1.0&quot;, features = [&quot;postgres&quot;, &quot;chrono&quot;] }

This goes for Diesel version 1.x as well.

huangapple
  • 本文由 发表于 2023年6月1日 23:00:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76383269.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定