diesel-rs/diesel

`get_result` should return an error if multiple rows are returned

Open

#2 446 ouverte le 29 juin 2020

Voir sur GitHub
 (2 commentaires) (8 réactions) (0 assignés)Rust (1 003 forks)batch import
documentationhelp wanted

Métriques du dépôt

Stars
 (12 054 stars)
Métriques de merge PR
 (Merge moyen 30j 16h) (30 PRs mergées en 30 j)

Description

Versions

All

Problem Description

RunQueryDsl provides several methods for accessing query results:

  • execute (returns number of rows affected)
  • load/get_results (returns all rows)
  • get_result (returns a single row)
  • first (returns the first row)

I expected that get_result would return an error if multiple rows are returned (to differentiate it from first). However, it seems that it will just return the first row. Other ORMs (SQLalchemy, Django ORM) will raise an exception in this case.

This is dangerous: if code is using get_result then it clearly expects a single row to be returned, and if multiple rows are returned, it's a sign that the query may be wrong, or accidentally missing filters which should be present. If this expectation is invalid, then diesel should report an error sooner rather than later.

Unfortunately, this is a breaking change to fix. Personally I think that's still worth it due to the danger not fixing it poses. However, I would also be OK with the documentation being updated to call out this fact, and a new method being added to assert that a single item is returned.

Guide contributeur