Home > Net >  Gorm query formatting breaking with `%` - "expected 0 arguments, got 1"
Gorm query formatting breaking with `%` - "expected 0 arguments, got 1"

Time:06-14

I am getting the error "expected 0 arguments, got 1" querying postgres with the following line:

db = db.Where("LOWER(name) LIKE LOWER('%?%')", nameSearch)

Which works when I hard code values into it like so:

db = db.Where("LOWER(name) LIKE LOWER('%some-value%')")

Can anyone spot my issue here as I have many where conditions similarly formatted that work but this one, with the extra % is breaking.

CodePudding user response:

After a quick look at the docs, it seems like you should add the wildcards to the nameSearch variable: as shown here

db.Where("name LIKE ?", "%jin%").Find(&users)
// SELECT * FROM users WHERE name LIKE '%jin%';

So that would be:

db.Where("LOWER(name) LIKE LOWER(?)", fmt.Sprintf("%%%s%%", nameSearch))

Of course, you can just use "%" nameSearch "%"

  • Related