Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IN (?) expression not expanded when using pointer slice #6794

Open
goamaral opened this issue Jan 14, 2024 · 0 comments
Open

IN (?) expression not expanded when using pointer slice #6794

goamaral opened this issue Jan 14, 2024 · 0 comments
Assignees
Labels
type:with reproduction steps with reproduction steps

Comments

@goamaral
Copy link

goamaral commented Jan 14, 2024

GORM Playground Link

go-gorm/playground#682

Description

var id uint = 1
DB.Table("users").Where("id = ?", id).Find(&users)
DB.Table("users").Where("id = ?", &id).Find(&users)

When using a value or a pointer, this kind of code behaves the same way

ids := []uint{1, 2, 3}
DB.Table("users").Where("id IN (?)", ids).Find(&users)
DB.Table("users").Where("id IN (?)", &ids).Find(&users)

I would assume that this would also happen with slices.
With a simple slice, SELECT * WHERE id IN (?) gets expanded to SELECT * WHERE id IN (?,?,?).
But with a slice pointer, this does not happen.

This case with a pointer slice is not handled in https://github.com/go-gorm/gorm/blob/master/clause/expression.go#L41
I think the same happens with clause.Where

This case was specially annoying to pinpoint because the query returns this error Failed, got error: sql: converting argument $1 type: unsupported type []uint, a slice of uint. Later I found that this error was in the go sql package. Now makes sense, but at first glance I did not know if the error was in the gorm code or not.

Do you intend to support this case? If so, I think, I can implement it. Just need to know how is the more "gorm" like approach. I would suspect it would be reflection. I think I have seen code that does the slice pointer slice somewhere already.

@github-actions github-actions bot added the type:with reproduction steps with reproduction steps label Jan 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:with reproduction steps with reproduction steps
Projects
None yet
Development

No branches or pull requests

2 participants