Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ambiguous traits in QueryableByName derivation when two fields have same concrete type but different SQL types #4131

Closed
3 tasks done
fiadliel opened this issue Jul 26, 2024 · 0 comments · Fixed by #4132
Closed
3 tasks done
Labels

Comments

@fiadliel
Copy link
Contributor

fiadliel commented Jul 26, 2024

Setup

Versions

  • Rust: 1.80.0
  • Diesel: 2.2.2
  • Database: PostgreSQL
  • **Operating System: ** MacOS

Feature Flags

  • diesel: postgres

Problem Description

What are you trying to accomplish?

Attempting to use QueryableByName + sql_query. In this case, two fields have the same type in the struct, but different SQL types.

Example:

#[derive(QueryableByName)]
#[diesel(check_for_backend(Pg))]
struct StructWithTextAndCitext {
    #[diesel(sql_type = sql_types::Text)]
    name: String,
    #[diesel(sql_type = sql_types::Citext)]
    email: String,
}

Alternative example (this also requires the serde_json feature of diesel):

#[derive(QueryableByName)]
#[diesel(check_for_backend(Pg))]
struct StructWithJsonAndJsonb {
    #[diesel(sql_type = sql_types::Json)]
    v1: serde_json::Value,
    #[diesel(sql_type = sql_types::Jsonb)]
    v2: serde_json::Value,
}

What is the expected output?

The code compiles, and creates a useful QueryableByName trait implementation.

What is the actual output?

In the first case above, the error is:

error[E0283]: type annotations needed
   --> src/main.rs:11:10
    |
11  | #[derive(QueryableByName)]
    |          ^^^^^^^^^^^^^^^ cannot infer type
    |
    = note: cannot satisfy `String: FromSql<_, __DB>`
    = help: the following types implement trait `FromSql<A, DB>`:
              <String as FromSql<Citext, Pg>>
              <String as FromSql<ST, DB>>
note: required by a bound in `diesel::row::NamedRow::get`
   --> /xxx/.cargo/registry/src/index.crates.io-6f17d22bba15001f/diesel-2.2.2/src/row.rs:133:12
    |
131 |     fn get<ST, T>(&self, column_name: &str) -> deserialize::Result<T>
    |        --- required by a bound in this associated function
132 |     where
133 |         T: FromSql<ST, DB>;
    |            ^^^^^^^^^^^^^^^ required by this bound in `NamedRow::get`
    = note: this error originates in the derive macro `QueryableByName` (in Nightly builds, run with -Z macro-backtrace for more info)

In the second case, the error is:

error[E0283]: type annotations needed
   --> src/main.rs:11:10
    |
11  | #[derive(QueryableByName)]
    |          ^^^^^^^^^^^^^^^ cannot infer type
    |
    = note: cannot satisfy `Value: FromSql<_, __DB>`
    = help: the following types implement trait `FromSql<A, DB>`:
              <Value as FromSql<Json, Pg>>
              <Value as FromSql<Jsonb, Pg>>
note: required by a bound in `diesel::row::NamedRow::get`
   --> /xxx/.cargo/registry/src/index.crates.io-6f17d22bba15001f/diesel-2.2.2/src/row.rs:133:12
    |
131 |     fn get<ST, T>(&self, column_name: &str) -> deserialize::Result<T>
    |        --- required by a bound in this associated function
132 |     where
133 |         T: FromSql<ST, DB>;
    |            ^^^^^^^^^^^^^^^ required by this bound in `NamedRow::get`
    = note: this error originates in the derive macro `QueryableByName` (in Nightly builds, run with -Z macro-backtrace for more info)

Steps to reproduce

Write the code above, with diesel dependency, and features postgres (and serde_json in the second case). If added to src/main.rs, you should see the above compile errors.

Checklist

  • I have already looked over the issue tracker and the discussion forum for similar possible closed issues.
  • This issue can be reproduced on Rust's stable channel. (Your issue will be
    closed if this is not the case)
  • This issue can be reproduced without requiring a third party crate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant