Rust diesel trait `load_dsl::private::CompatibleType〈PodcastEpisode,Sqlite>`未实现为`Untyped

sc4hvdpw  于 2023-04-30  发布在  SQLite
关注(0)|答案(1)|浏览(119)

我不知道为什么下面的播客情节结构

#[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<Date>)]
    pub(crate) download_time: Option<NaiveDateTime>
}

在编写以下内容时给出此错误。错误来自conn,并表示以下内容

error[E0277]: the trait bound `Untyped: load_dsl::private::CompatibleType<PodcastEpisode, Sqlite>` is not satisfied
    --> src\db.rs:755:47
     |
755  |         let res1 = res.load::<PodcastEpisode>(conn).expect("Error loading \
     |                        ----                   ^^^^ the trait `load_dsl::private::CompatibleType<PodcastEpisode, Sqlite>` is not implemented for `Untyped`
     |                        |
     |                        required by a bound introduced by this call
     |
     = help: the trait `load_dsl::private::CompatibleType<U, DB>` is implemented for `Untyped`
     = note: required for `diesel::query_builder::sql_query::UncheckedBind<SqlQuery, &std::string::String, diesel::sql_types::Text>` to implement `LoadQuery<'_, _, PodcastEpisode>`
note: required by a bound in `diesel::RunQueryDsl::load`
    --> <path>\src\github.com-1ecc6299db9ec823\diesel-2.0.3\src\query_dsl\mod.rs:1499:15
     |
1499 |         Self: LoadQuery<'query, Conn, U>,
     |               ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `RunQueryDsl::load`
pub fn get_timeline(username_to_search: String, conn: &mut SqliteConnection) {
        let podcast_timeline = sql_query("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");

        let res = podcast_timeline.bind::<Text, _>(&username_to_search);

        let res1 = res.load::<PodcastEpisode>(conn).expect("Error loading \
        podcast \
        episode by id");

    }

同时,如果我添加这个结构,应用程序将启动。它是sql查询的错误类型,但应用程序启动:

#[derive(Serialize, Deserialize, Queryable, QueryableByName, Clone, ToSchema)]
#[serde(rename_all = "camelCase")]
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
}

相关表格定义:
播客

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

联系我们

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

播客节目

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

完整的代码可以看到here

dsekswqp

dsekswqp1#

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

download_time -> Nullable<Timestamp>,

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

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

Nullable<Date>Nullable<Timestamp>不一样。
您可能打算在结构定义中写入Nullable<Timestamp>

相关问题