Masterminds / squirrel

Fluent SQL generation for golang
Other
6.67k stars 458 forks source link

error when using sqlx #341

Open keeprockin2019 opened 1 year ago

keeprockin2019 commented 1 year ago
query :=  sq.sq.Select(`COALESCE(MAX(ID),0)`).From(`channel`)
if len(name) != 0 {
     query = query.Where(sq.Eq{"name":name})
}
if true {
   query = query.Where(sq.Eq{"active":active})
}
// result
// sql  = SELECT COALESCE(MAX(ID),0) FROM channel WHERE name = ?
// args = []interface{} & 0 = xxxx
sql,args,err := query.ToSql()
if err != nil {
   return err
}
err = sqlx.GetContext(ctx,&data,sql,args...)
if err != nil{
    // output
    // pq: syntax error at end of input
}
keeprockin2019 commented 1 year ago

I checked the sql is correct, does anyone know why this is?

jlautman commented 1 year ago

What sql engine are you using? ? syntax won't work on PostGreSQL.

It's also suspicious that I don't see a WHERE active = ? in the sql you're showing here.

How did you check the sql is correct? Did you evaluate it directly against your database?