doug-martin / goqu

SQL builder and query library for golang
http://doug-martin.github.io/goqu/
MIT License
2.37k stars 207 forks source link

Subqueries in Update().Set() with Postgres dialect generates incorrect query #413

Open afv1 opened 6 months ago

afv1 commented 6 months ago

Describe the bug Subqueries in Update().Set() with Postgres dialect generates incorrect query: '?' instead of '$1'

To Reproduce

testDS := goqu.Dialect("postgres").Update("test.table_1").
    Set(goqu.Record{
        "sub": goqu.Select("id").
            From("test.table_1").
            Where(goqu.C("from").Eq(from)),
    }).
    From("test.table_2").
    Where(
        goqu.C("test_id").Eq(id),
        goqu.C("to").Eq(to),
    )

fmt.Println(testDS.Prepared(true).ToSQL())

// Output:
// UPDATE "test"."table_1" SET "sub"=(SELECT "id" FROM "test"."table_1" WHERE ("from" = ?)) FROM "test"."table_2"
// WHERE (("test_id" = $2) AND ("to" = $3)) [1 12 2] <nil>

Expected behavior Expected output:

UPDATE "test"."table1" SET "huy"=(SELECT "id" FROM "t"."subtable_1" WHERE ("from" = $1)) FROM "test"."table_2" WHERE (("test_id" = $2) AND ("to" = $3)) [1 12 2] <nil>

But first parameter interpolated incorrectly for PG syntax '?'. Looks like it caused by sybquery in Setect(...)

Dialect:

Additional context Add any other context about the problem here.