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

error message could explain `execute` vs `load` #1136

Closed
jonathanstrong opened this Issue Aug 31, 2017 · 3 comments

Comments

Projects
None yet
4 participants
@jonathanstrong

jonathanstrong commented Aug 31, 2017

Setup

Versions

  • Rust: nightly 1.21 2017-08-29
  • Diesel: 0.14
  • Database: postgresql 9.5
  • Operating System ubuntu 16.04

Feature Flags

  • diesel: postgres, uuid, serde_json, chrono
  • diesel_codegen: postgres

Problem Description

It took me a long time to figure out that execute returns at most one value (I'm assuming) while load returns (or can return) a collection of values.

I got the following error message:

error[E0308]: mismatched types
  --> src/main.rs:95:9
   |
95 | /         sql::<(Integer, pg_types::Uuid,)>(query)
96 | |             .execute(&db)
97 | |             .expect("query failed");
   | |_______________________________________^ expected struct `std::vec::Vec`, found usize
   |
   = note: expected type `std::vec::Vec<(i32, uuid::Uuid)>`
              found type `usize`

(You can't see it in the error message, but I was trying to assign the query result to a Vec<(i32, uuid::Uuid)>).

It would be helpful if this error message could offer some hint about the difference between execute vs load. Just thought I would share a point of confusion from someone trying to learn the library. Thanks!

Checklist

  • [ yes ] I have already looked over the issue tracker for similar issues.
@sgrif

This comment has been minimized.

Member

sgrif commented Sep 1, 2017

We don't have control over the error message, but we do call this out in the getting started guide. We should make that same call out in the docs for load

brandur added a commit to brandur/diesel that referenced this issue Feb 8, 2018

Clarify use of `execute` versus `load` in documentation
Adds cross-references to the documentation of the `execute` and `load`
functions that help clarify when it's appropriate to use each one, and
suggests the use of the other when it's not.

Fixes diesel-rs#1136.
@brandur

This comment has been minimized.

Contributor

brandur commented Feb 8, 2018

I took a stab at adding a little more documentation in #1548 (as suggested).

IMO though, the original reporter here has a point in that this goes a little beyond a documentation problem. I've been using Diesel fairly seriously for a few weeks now and it's quite a common pattern to have a compiler protect you from misuse, but to do so in a way that doesn't help give you much of an idea as to what's actually wrong.

You'll probably eventually solve the problem, but you do so by changing things until you accidentally get it right, investing lots of time reading documentation, or by cloning Diesel's source code and examining its source and examples in the test suite (I've resorted to this last technique about a hundred times). Compiler errors are often of minimal help.

I don't really have a suggestion on how to fix this, but I wanted to second the fact that it's a fairly major problem, especially for newcomers to Rust and Diesel who may not be as practiced at unwinding these types of error messages.

@brandur

This comment has been minimized.

Contributor

brandur commented Feb 10, 2018

Another data point: #1551. Diesel is doing something useful by not allowing a BIGINT to be loaded into an i32, but a new user is quite confused by the error message that's produced.

brandur added a commit to brandur/diesel that referenced this issue Feb 15, 2018

Clarify use of `execute` versus `load` in documentation
Adds cross-references to the documentation of the `execute` and `load`
functions that help clarify when it's appropriate to use each one, and
suggests the use of the other when it's not.

Fixes diesel-rs#1136.

@sgrif sgrif closed this in #1548 Feb 16, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment