Search code examples
mysqlgogo-gorm

Double parenthesis for where clause using "IN" at gorm


I'm using the where clause to update a value at my database using gorm

db.Session(&gorm.Session{FullSaveAssociations: true}).Where("id IN (?)", ids).Updates(users)

The ids is a slice of ids and the user is the struct

The problem is that I'm getting this error

Error 1241 (21000): Operand should contain 1 column(s)

The sql that gorm generates is this one. It looks like that it creates a double parenthesis that is giving this error. But I don't know how to fix this

UPDATE `users` SET `status`='CANCELED',`updated_at`='2023-12-12 20:39:40.904' WHERE id IN (('066a75df-ba11-49c8-9b39-b1cce029760e','5f95f93e-94a5-46d1-86eb-dde83437ea26'))

EDIT 1

I had the same effect removing the () from (?)

db.Session(&gorm.Session{FullSaveAssociations: true}).Where("id IN ?", ids).Updates(users)

UPDATE `users` SET `status`='CANCELED',`updated_at`='2023-12-12 21:18:46.537' WHERE id IN (('066a75df-ba11-49c8-9b39-b1cce029760e','5f95f93e-94a5-46d1-86eb-dde83437ea26'))

Error 1241 (21000): Operand should contain 1 column(s)


Solution

  • I found the answer here https://github.com/go-gorm/gorm/issues/5014 and https://gorm.io/docs/sql_builder.html#Clauses

    Using this clauses I could do something like this

    query.Clauses(
                clause.Where{
                    Exprs: []clause.Expression{
                        clause.Expr{
                            SQL:                "id IN ?",
                            Vars:               []interface{}{ids},
                            WithoutParentheses: true,
                        },
                    },
                },
            )