diesel-rs/diesel

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

开放

#2,446 创建于 2020年6月29日

 (2 条评论) (8 个反应) (0 位负责人)Rust (1,003 个派生)batch import
documentationhelp wanted

仓库指标

星标
 (12,054 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

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.

贡献者指南