Skip to content
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

about use select and distinct #7111

Open
GwenLily opened this issue Jul 17, 2024 · 0 comments
Open

about use select and distinct #7111

GwenLily opened this issue Jul 17, 2024 · 0 comments
Assignees
Labels
type:question general questions

Comments

@GwenLily
Copy link

Your Question


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:

  1. If I call Distinct() without any parameters before Select(...), and do not add Distinct in the Select(...) method, it still achieves the distinct effect.
  2. 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 results
var RuleList []biz.RuleDetailInfo
err := queryCondition.Select(blackWhiteDetailQuery.ALL, blackWhiteQuery.EntityIdentType, blackWhiteQuery.SourcePlatform).
        Distinct(blackWhiteDetailQuery.ALL, blackWhiteQuery.EntityIdentType, blackWhiteQuery.SourcePlatform).
        Scan(&RuleList)

queryCondition.Distinct().Order("id desc")
var RuleList []biz.RuleDetailInfo
err := queryCondition.Select(blackWhiteDetailQuery.ALL, blackWhiteQuery.EntityIdentType, blackWhiteQuery.SourcePlatform).
        Scan(&RuleList)

queryCondition.Distinct(blackWhiteDetailQuery.ID).Order("id desc")
var RuleList []biz.RuleDetailInfo
err := queryCondition.Select(blackWhiteDetailQuery.ALL, blackWhiteQuery.EntityIdentType, blackWhiteQuery.SourcePlatform).
        Scan(&RuleList)

// This does not work if fields in Distinct do not match Select
var RuleList []biz.RuleDetailInfo
err := 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

@GwenLily GwenLily added the type:question general questions label Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question general questions
Projects
None yet
Development

No branches or pull requests

2 participants