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

Grouped conditions in scopes results in duplicative where conds #7136

Open
cbaker opened this issue Aug 1, 2024 · 4 comments · May be fixed by #7198
Open

Grouped conditions in scopes results in duplicative where conds #7136

cbaker opened this issue Aug 1, 2024 · 4 comments · May be fixed by #7198
Assignees
Labels
type:with reproduction steps with reproduction steps

Comments

@cbaker
Copy link

cbaker commented Aug 1, 2024

GORM Playground Link

go-gorm/playground#754

Description

I was under the impression that using a grouped where cond in a query scope, would apply to the query only 1 time.

The results I'm seeing from applying a grouped cond in a query scope such as:

func ScopeSomething(a,b string) func (*gorm.DB) *gorm.DB {
  return func (db *gorm.DB) *gorm.DB {
    return db.Where(
      // would anticipate the following being added to query as ("field_a" = ? OR "field_b" = ?)
      db.Where("field_a", a).Or("field_b", b),
    )
  }
}

This generates a query such as:

SELECT * FROM sometable WHERE field_a = ? or field_b = ? AND (field_a = ? OR field_b = ?)

The desired behavior can be achieved by doing the following but it doesn't feel as declarative.

func ScopeSomething(a,b string) func (*gorm.DB) *gorm.DB {
  return func (db *gorm.DB) *gorm.DB {
    return db.Where("field_a = ? OR field_b = ?", a, b)
  }
}
@github-actions github-actions bot added the type:with reproduction steps with reproduction steps label Aug 1, 2024
@OliverChao
Copy link

Right. I have noticed this issue, too.
Is there any pointers to fixing this bug?

@cbaker
Copy link
Author

cbaker commented Sep 11, 2024

Right. I have noticed this issue, too. Is there any pointers to fixing this bug?

Unfortunately, I haven't made the time to dig into it myself yet.
It strikes me as odd that it actually does generate part of the query in the anticipated manner but also appends the redundant call. I suspect that both calls to db.Where are adding conditions independently somehow.

@a631807682
Copy link
Member

Use session to avoid repeated changes to the db instance
https://gorm.io/docs/method_chaining.html#Reusability-and-Safety

@cbaker
Copy link
Author

cbaker commented Sep 19, 2024

Use session to avoid repeated changes to the db instance https://gorm.io/docs/method_chaining.html#Reusability-and-Safety

I don't believe this is applicable to the documentation you've linked since we aren't trying to re-use a gorm.DB and group conditions are explicitly listed in the documentation as a feature:
https://gorm.io/docs/advanced_query.html#Group-Conditions

Please consider taking a second look.

@a631807682 a631807682 reopened this Sep 20, 2024
@a631807682 a631807682 linked a pull request Sep 20, 2024 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:with reproduction steps with reproduction steps
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants