Rust diesel the trait `load_dsl::private::CompatibleType<PodcastEpisode, Sqlite>` is not implemented for `Untyped

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

Rust diesel the trait `load_dsl::private::CompatibleType<PodcastEpisode, Sqlite>` is not implemented for `Untyped

问题

The provided code appears to be in Rust and involves database interactions using Diesel. Here's the translated content without the code:

无法找出以下 PodcastEpisode 结构体在写入以下内容时出现以下错误的原因。错误来自于 conn 并且指出以下内容:

同时,如果我添加这个结构体,应用程序就会启动。尽管该结构体不是 SQL 查询的正确类型,但应用程序可以启动。

相关的表定义如下:

Podcasts 表:

Favorites 表:

Podcast Episodes 表:

完整的代码可以在此处查看。

英文:

I can't find out why the following PodcastEpisode struct

#[derive(Queryable, Identifiable,QueryableByName, Selectable, Debug, PartialEq, Clone, ToSchema,
Serialize, Deserialize)]
pub struct PodcastEpisode {
    #[diesel(sql_type = Integer)]
    pub(crate) id: i32,
    #[diesel(sql_type = Integer)]
    pub(crate) podcast_id: i32,
    #[diesel(sql_type = Text)]
    pub(crate) episode_id: String,
    #[diesel(sql_type = Text)]
    pub(crate) name: String,
    #[diesel(sql_type = Text)]
    pub(crate) url: String,
    #[diesel(sql_type = Text)]
    pub(crate) date_of_recording: String,
    #[diesel(sql_type = Text)]
    pub image_url: String,
    #[diesel(sql_type = Integer)]
    pub total_time: i32,
    #[diesel(sql_type = Text)]
    pub(crate) local_url: String,
    #[diesel(sql_type = Text)]
    pub(crate) local_image_url: String,
    #[diesel(sql_type = Text)]
    pub(crate) description: String,
    #[diesel(sql_type = Text)]
    pub(crate) status: String,
    #[diesel(sql_type = Nullable&lt;Date&gt;)]
    pub(crate) download_time: Option&lt;NaiveDateTime&gt;
}

is giving this error when the following is written. The error comes from conn and is saying the following

error[E0277]: the trait bound `Untyped: load_dsl::private::CompatibleType&lt;PodcastEpisode, Sqlite&gt;` is not satisfied
    --&gt; src\db.rs:755:47
     |
755  |         let res1 = res.load::&lt;PodcastEpisode&gt;(conn).expect(&quot;Error loading \
     |                        ----                   ^^^^ the trait `load_dsl::private::CompatibleType&lt;PodcastEpisode, Sqlite&gt;` is not implemented for `Untyped`
     |                        |
     |                        required by a bound introduced by this call
     |
     = help: the trait `load_dsl::private::CompatibleType&lt;U, DB&gt;` is implemented for `Untyped`
     = note: required for `diesel::query_builder::sql_query::UncheckedBind&lt;SqlQuery, &amp;std::string::String, diesel::sql_types::Text&gt;` to implement `LoadQuery&lt;&#39;_, _, PodcastEpisode&gt;`
note: required by a bound in `diesel::RunQueryDsl::load`
    --&gt; &lt;path&gt;\src\github.com-1ecc6299db9ec823\diesel-2.0.3\src\query_dsl\mod.rs:1499:15
     |
1499 |         Self: LoadQuery&lt;&#39;query, Conn, U&gt;,
     |               ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `RunQueryDsl::load`

    pub fn get_timeline(username_to_search: String, conn: &amp;mut SqliteConnection) {
        let podcast_timeline = sql_query(&quot;SELECT podcast_episodes FROM podcasts, podcast_episodes, \
        favorites \
        WHERE podcasts.id = podcast_episodes.podcast_id AND podcasts.id = favorites.podcast_id AND favorites.username=? AND favored=1 ORDER BY podcast_episodes.date_of_recording DESC&quot;);

        let res = podcast_timeline.bind::&lt;Text, _&gt;(&amp;username_to_search);


        let res1 = res.load::&lt;PodcastEpisode&gt;(conn).expect(&quot;Error loading \
        podcast \
        episode by id&quot;);


    }

Meanwhile if I add this struct the application starts. It is the wrong type for the sql query but the application starts:

#[derive(Serialize, Deserialize, Queryable, QueryableByName, Clone, ToSchema)]
#[serde(rename_all = &quot;camelCase&quot;)]
pub struct PodcastHistoryItem {
    #[diesel(sql_type = Integer)]
    pub id: i32,
    #[diesel(sql_type = Integer)]
    pub podcast_id: i32,
    #[diesel(sql_type = Text)]
    pub episode_id: String,
    #[diesel(sql_type = Integer)]
    pub watched_time: i32,
    #[diesel(sql_type = Text)]
    pub date: String,
    #[diesel(sql_type = Text)]
    pub username: String
}

Relevant table definitions:

Podcasts

diesel::table! {
    podcasts (id) {
        id -&gt; Integer,
        name -&gt; Text,
        directory_id -&gt; Text,
        rssfeed -&gt; Text,
        image_url -&gt; Text,
        summary -&gt; Nullable&lt;Text&gt;,
        language -&gt; Nullable&lt;Text&gt;,
        explicit -&gt; Nullable&lt;Text&gt;,
        keywords -&gt; Nullable&lt;Text&gt;,
        last_build_date -&gt; Nullable&lt;Text&gt;,
        author -&gt; Nullable&lt;Text&gt;,
        active -&gt; Bool,
        original_image_url -&gt; Text,
        directory_name -&gt; Text,
    }
}

Favorites:

diesel::table! {
    favorites (username, podcast_id) {
        username -&gt; Text,
        podcast_id -&gt; Integer,
        favored -&gt; Bool,
    }
}

Podcast Episodes

diesel::table! {
    podcast_episodes (id) {
        id -&gt; Integer,
        podcast_id -&gt; Integer,
        episode_id -&gt; Text,
        name -&gt; Text,
        url -&gt; Text,
        date_of_recording -&gt; Text,
        image_url -&gt; Text,
        total_time -&gt; Integer,
        local_url -&gt; Text,
        local_image_url -&gt; Text,
        description -&gt; Text,
        status -&gt; Text,
        download_time -&gt; Nullable&lt;Timestamp&gt;,
    }
}

The full code can be seen here

答案1

得分: 1

table! 宏中定义的类型存在不匹配,download_time 被定义为:

    download_time -> Nullable<Timestamp>,

而在结构体中,您将其定义为:

    #[diesel(sql_type = Nullable<Date>)]
    pub(crate) download_time: Option<NaiveDateTime>

Nullable<Date>Nullable<Timestamp> 不一样。

您可能想在结构体定义中写成 Nullable<Timestamp>

英文:

There is a mismatch in the defined types, at the table! macro download_time is defined as:

    download_time -&gt; Nullable&lt;Timestamp&gt;,

In the struct you define it as:

    #[diesel(sql_type = Nullable&lt;Date&gt;)]
    pub(crate) download_time: Option&lt;NaiveDateTime&gt;

Nullable&lt;Date&gt; and Nullable&lt;Timestamp&gt; are not the same.

You probably meant to write Nullable&lt;Timestamp&gt; in the struct definition.

huangapple
  • 本文由 发表于 2023年4月20日 05:08:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058817.html
匿名

发表评论

匿名网友

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

确定