bun icon indicating copy to clipboard operation
bun copied to clipboard

Missing starting parenthesis when calling WhereGroup first

Open rdujardin opened this issue 5 years ago • 2 comments

Hello,

Here's an example of the issue:

err := db.NewSelect().
    Model(book).
    WhereGroup("", func (q *bun.Query) {
        q.Where("a = 1").Where("b = 1")
    }).
    WhereGroup(" OR ", func(q *bun.Query) {
        q.Where("a = 2").Where("b = 2")
    }).
    Scan(ctx)

Leads to a query like this:

SELECT "books"."a", "books.b" FROM "books" AS "books" WHERE (a = 1) AND (b = 1)) OR ((a = 2) AND (b = 2))

It's missing the first opening parenthesis.

I quickly fixed it by changing query_base.go:641 from:

if i > 0 {

To:

if i > 0 || where.Sep == "(" {

Also, the documentation https://bun.uptrace.dev/guide/queries.html#select is not up to date about WhereGroup.

Thanks.

rdujardin avatar Jul 28 '21 15:07 rdujardin

(Note that I tested the same with go-pg and it works correctly).

rdujardin avatar Jul 28 '21 19:07 rdujardin

Thanks for the details report - this should be fixed in v0.3.8

vmihailenco avatar Jul 29 '21 06:07 vmihailenco