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.

貢獻者指南