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

test #754

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft

test #754

wants to merge 4 commits into from

Conversation

cbaker
Copy link

@cbaker cbaker commented Aug 1, 2024

Explain your user case and expected results

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 proper.

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)
  }
}

Actual Test QB:

gdb.Model(&User{}).Where("active", true).Scopes(func(d *gorm.DB) *gorm.DB {
  return d.Where(
    d.Where("id", id).Or("name", name),
  )
}).First(&out)

Actual Test Query Output:

SELECT * FROM "users" WHERE ("active" = $1 AND "id" = $2 OR "name" = $3 AND ("active" = $4 AND "id" = $5 OR "name" = $6)) AND "users"."deleted_at" IS NULL ORDER BY "users"."id" LIMIT $7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant