Skip to content

Grouped conditions in scopes results in duplicative where conds #7136

@cbaker

Description

@cbaker

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions