Link0
Link0

Reputation: 669

Rust sqlx: there is no query finalizers

The issue is when I type code like this.

let data = sqlx::query("SELECT id, name FROM test")
        .fetch_all(&pool)
        .await;

I get an no method named `fetch_all` found for struct `sqlx_core::query::Query<'_, _>` in the current scope error. Following official getting started readme. This is also applicable to the query_as method. I suspect that for some reason the compiler doesn't "see" sqlx::Query trait methods, but I don't know how to bring them in scope.

Although when I use macro sqlx::query!, .fetch_all does exist.

Also, there is an inconvenience that rust-analyzer LSP tells me that the type of the data variable is std::result::Result<[type error], [type error]>, which removes any possibility of using autocompletion and type checking, other than running (notoriously slow) rust compiler.

P.S. I use PostgreSQL as a database solution, if that may help.

P.S.S. env variable DATABASE_URL is set at the compile-time and is correct, so macros do all of the compile-time validation stuff.

Upvotes: 6

Views: 1988

Answers (1)

Link0
Link0

Reputation: 669

Browsing docs.rs a bit, I found that type sqlx::Query does not even have finalizers other than fetch. As for sqlx::query_as, the trait I was looking for was called PgQueryAs, which actually contains fetch_all, fetch_optional, fetch_one, etc. And adding use sqlx::postgress::PgQueryAs fixed the problem for me.

Despite that, I still don't know a solution for evaluating macros to get decent types in my IDE.

Upvotes: 1

Related Questions