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

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

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

  1. #[derive(Queryable, Identifiable,QueryableByName, Selectable, Debug, PartialEq, Clone, ToSchema,
  2. Serialize, Deserialize)]
  3. pub struct PodcastEpisode {
  4. #[diesel(sql_type = Integer)]
  5. pub(crate) id: i32,
  6. #[diesel(sql_type = Integer)]
  7. pub(crate) podcast_id: i32,
  8. #[diesel(sql_type = Text)]
  9. pub(crate) episode_id: String,
  10. #[diesel(sql_type = Text)]
  11. pub(crate) name: String,
  12. #[diesel(sql_type = Text)]
  13. pub(crate) url: String,
  14. #[diesel(sql_type = Text)]
  15. pub(crate) date_of_recording: String,
  16. #[diesel(sql_type = Text)]
  17. pub image_url: String,
  18. #[diesel(sql_type = Integer)]
  19. pub total_time: i32,
  20. #[diesel(sql_type = Text)]
  21. pub(crate) local_url: String,
  22. #[diesel(sql_type = Text)]
  23. pub(crate) local_image_url: String,
  24. #[diesel(sql_type = Text)]
  25. pub(crate) description: String,
  26. #[diesel(sql_type = Text)]
  27. pub(crate) status: String,
  28. #[diesel(sql_type = Nullable&lt;Date&gt;)]
  29. pub(crate) download_time: Option&lt;NaiveDateTime&gt;
  30. }

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

  1. error[E0277]: the trait bound `Untyped: load_dsl::private::CompatibleType&lt;PodcastEpisode, Sqlite&gt;` is not satisfied
  2. --&gt; src\db.rs:755:47
  3. |
  4. 755 | let res1 = res.load::&lt;PodcastEpisode&gt;(conn).expect(&quot;Error loading \
  5. | ---- ^^^^ the trait `load_dsl::private::CompatibleType&lt;PodcastEpisode, Sqlite&gt;` is not implemented for `Untyped`
  6. | |
  7. | required by a bound introduced by this call
  8. |
  9. = help: the trait `load_dsl::private::CompatibleType&lt;U, DB&gt;` is implemented for `Untyped`
  10. = 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;`
  11. note: required by a bound in `diesel::RunQueryDsl::load`
  12. --&gt; &lt;path&gt;\src\github.com-1ecc6299db9ec823\diesel-2.0.3\src\query_dsl\mod.rs:1499:15
  13. |
  14. 1499 | Self: LoadQuery&lt;&#39;query, Conn, U&gt;,
  15. | ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `RunQueryDsl::load`
  1. pub fn get_timeline(username_to_search: String, conn: &amp;mut SqliteConnection) {
  2. let podcast_timeline = sql_query(&quot;SELECT podcast_episodes FROM podcasts, podcast_episodes, \
  3. favorites \
  4. 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;);
  5. let res = podcast_timeline.bind::&lt;Text, _&gt;(&amp;username_to_search);
  6. let res1 = res.load::&lt;PodcastEpisode&gt;(conn).expect(&quot;Error loading \
  7. podcast \
  8. episode by id&quot;);
  9. }

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

  1. #[derive(Serialize, Deserialize, Queryable, QueryableByName, Clone, ToSchema)]
  2. #[serde(rename_all = &quot;camelCase&quot;)]
  3. pub struct PodcastHistoryItem {
  4. #[diesel(sql_type = Integer)]
  5. pub id: i32,
  6. #[diesel(sql_type = Integer)]
  7. pub podcast_id: i32,
  8. #[diesel(sql_type = Text)]
  9. pub episode_id: String,
  10. #[diesel(sql_type = Integer)]
  11. pub watched_time: i32,
  12. #[diesel(sql_type = Text)]
  13. pub date: String,
  14. #[diesel(sql_type = Text)]
  15. pub username: String
  16. }

Relevant table definitions:

Podcasts

  1. diesel::table! {
  2. podcasts (id) {
  3. id -&gt; Integer,
  4. name -&gt; Text,
  5. directory_id -&gt; Text,
  6. rssfeed -&gt; Text,
  7. image_url -&gt; Text,
  8. summary -&gt; Nullable&lt;Text&gt;,
  9. language -&gt; Nullable&lt;Text&gt;,
  10. explicit -&gt; Nullable&lt;Text&gt;,
  11. keywords -&gt; Nullable&lt;Text&gt;,
  12. last_build_date -&gt; Nullable&lt;Text&gt;,
  13. author -&gt; Nullable&lt;Text&gt;,
  14. active -&gt; Bool,
  15. original_image_url -&gt; Text,
  16. directory_name -&gt; Text,
  17. }
  18. }

Favorites:

  1. diesel::table! {
  2. favorites (username, podcast_id) {
  3. username -&gt; Text,
  4. podcast_id -&gt; Integer,
  5. favored -&gt; Bool,
  6. }
  7. }

Podcast Episodes

  1. diesel::table! {
  2. podcast_episodes (id) {
  3. id -&gt; Integer,
  4. podcast_id -&gt; Integer,
  5. episode_id -&gt; Text,
  6. name -&gt; Text,
  7. url -&gt; Text,
  8. date_of_recording -&gt; Text,
  9. image_url -&gt; Text,
  10. total_time -&gt; Integer,
  11. local_url -&gt; Text,
  12. local_image_url -&gt; Text,
  13. description -&gt; Text,
  14. status -&gt; Text,
  15. download_time -&gt; Nullable&lt;Timestamp&gt;,
  16. }
  17. }

The full code can be seen here

答案1

得分: 1

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

  1. download_time -> Nullable<Timestamp>,

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

  1. #[diesel(sql_type = Nullable<Date>)]
  2. 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:

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

In the struct you define it as:

  1. #[diesel(sql_type = Nullable&lt;Date&gt;)]
  2. 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:

确定