You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Title: Inconsistent Behavior of Distinct with and without Parameters in GORM
Description:
I've noticed some inconsistent behavior when using the Distinct method in GORM. According to the official documentation, Distinct should take the fields to be used for distinct selection as parameters. For example:
db.Distinct("name", "age").Order("name, age desc").Find(&results)
However, I have observed the following behavior:
If I call Distinct() without any parameters before Select(...), and do not add Distinct in the Select(...) method, it still achieves the distinct effect.
Conversely, if I add Distinct in the Select(...) method, I must include the same fields in the Distinct call to achieve the desired effect. If the fields in Distinct do not match those in Select, the distinct effect does not work.
Here is an example to illustrate:
// This works and achieves distinct resultsvarRuleList []biz.RuleDetailInfoerr:=queryCondition.Select(blackWhiteDetailQuery.ALL, blackWhiteQuery.EntityIdentType, blackWhiteQuery.SourcePlatform).
Distinct(blackWhiteDetailQuery.ALL, blackWhiteQuery.EntityIdentType, blackWhiteQuery.SourcePlatform).
Scan(&RuleList)
queryCondition.Distinct().Order("id desc")
varRuleList []biz.RuleDetailInfoerr:=queryCondition.Select(blackWhiteDetailQuery.ALL, blackWhiteQuery.EntityIdentType, blackWhiteQuery.SourcePlatform).
Scan(&RuleList)
queryCondition.Distinct(blackWhiteDetailQuery.ID).Order("id desc")
varRuleList []biz.RuleDetailInfoerr:=queryCondition.Select(blackWhiteDetailQuery.ALL, blackWhiteQuery.EntityIdentType, blackWhiteQuery.SourcePlatform).
Scan(&RuleList)
// This does not work if fields in Distinct do not match SelectvarRuleList []biz.RuleDetailInfoerr:=queryCondition.Select(blackWhiteDetailQuery.ALL, blackWhiteQuery.EntityIdentType, blackWhiteQuery.SourcePlatform).
Distinct().
Scan(&RuleList)
I don't quite understand why? The documentation doesn't clearly explain this distinction, and it would be helpful to have clarification on how Distinct is intended to work in these scenarios.
Thank you!
The document you expected this should be explained
Expected answer
The text was updated successfully, but these errors were encountered:
Your Question
Title: Inconsistent Behavior of
Distinct
with and without Parameters in GORMDescription:
I've noticed some inconsistent behavior when using the
Distinct
method in GORM. According to the official documentation,Distinct
should take the fields to be used for distinct selection as parameters. For example:However, I have observed the following behavior:
Distinct()
without any parameters beforeSelect(...)
, and do not addDistinct
in theSelect(...)
method, it still achieves the distinct effect.Distinct
in theSelect(...)
method, I must include the same fields in theDistinct
call to achieve the desired effect. If the fields inDistinct
do not match those inSelect
, the distinct effect does not work.Here is an example to illustrate:
I don't quite understand why? The documentation doesn't clearly explain this distinction, and it would be helpful to have clarification on how
Distinct
is intended to work in these scenarios.Thank you!
The document you expected this should be explained
Expected answer
The text was updated successfully, but these errors were encountered: