Skip to content

Postgres query with where in (subquery) wants to return every field as Option #1634

Open
@regularfellow

Description

@regularfellow

When selecting rows using WHERE IN (SUBQUERY) sqlx erroneously(?) tries to return the rows with every field as nullable:

https://github.com/regularfellow/todos-example/blob/c74f96319e15c2b181f140c0f7e497a5ccd8ac05/src/main.rs#L19-L22

struct Todo {
    id: i64,
    description: String,
    done: bool,
}

sqlx::query_as!(
    Todo,
    "SELECT * FROM todos WHERE id IN (SELECT todo_id FROM todo_things)"
)
➜  todos-example git:(master) cargo check
    Checking sqlx-example-postgres-connected-todos v0.1.0 (/home/github/todos-example)
error[E0308]: mismatched types
  --> src/main.rs:19:17
   |
19 |       let todos = sqlx::query_as!(
   |  _________________^
20 | |         Todo,
21 | |         "SELECT * FROM todos WHERE id IN (SELECT todo_id FROM todo_things)"
22 | |     )
   | |_____^ expected `i64`, found enum `Option`
   |
   = note: expected type `i64`
              found enum `Option<i64>`
   = note: this error originates in the macro `$crate::sqlx_macros::expand_query` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0308]: mismatched types
  --> src/main.rs:19:17
   |
19 |       let todos = sqlx::query_as!(
   |  _________________^
20 | |         Todo,
21 | |         "SELECT * FROM todos WHERE id IN (SELECT todo_id FROM todo_things)"
22 | |     )
   | |_____^ expected struct `String`, found enum `Option`
   |
   = note: expected struct `String`
                found enum `Option<String>`
   = note: this error originates in the macro `$crate::sqlx_macros::expand_query` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0308]: mismatched types
  --> src/main.rs:19:17
   |
19 |       let todos = sqlx::query_as!(
   |  _________________^
20 | |         Todo,
21 | |         "SELECT * FROM todos WHERE id IN (SELECT todo_id FROM todo_things)"
22 | |     )
   | |_____^ expected `bool`, found enum `Option`
   |
   = note: expected type `bool`
              found enum `Option<bool>`
   = note: this error originates in the macro `$crate::sqlx_macros::expand_query` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0308`.
error: could not compile `sqlx-example-postgres-connected-todos` due to 3 previous errors

Example repo can be found from https://github.com/regularfellow/todos-example if helpful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions