GORM Playground Link
https://github.com/go-gorm/playground/pull/550
Description
This page:
https://gorm.io/docs/sql_builder.html
seems to indicate you can bind values of any type (string, int, etc) to a prepared statement - exp:
var users []User
db.Raw("UPDATE users SET name = ? WHERE age = ? RETURNING id, name", "jinzhu", 20).Scan(&users)
However, the tests in my PR fail:
Looks like the issue is in this bit of the code:
func (stmt *Statement) AddVar(writer clause.Writer, vars ...interface{}) {
which failed to match on any of the case statements - so it ended up in here:
func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{}) {
writer.WriteByte('?')
}
where the values are just failing to bind to the question marks (note that v is unreferenced in the above method).
Notes
I think there might be something missing in the instructions for setting up the gorm-playground locally. Specifically I think between step (3) and (4) you're supposed to run
docker-compose up
There is a README.md under gorm/tests (i.e. this one: https://github.com/go-gorm/gorm/blob/master/tests/README.md) that mentions "docker-compose up" - which isn't mentioned in the instructions in the top level README.md (i.e. this one: https://github.com/go-gorm/playground/blob/master/README.md).
Without running "docker-compose up" - you'll see errors like these:
1.
After forking the gorm playground and then running test.sh - I got an auth error for mysql:
2022/12/10 15:05:02 testing sqlserver...
2022/12/10 15:05:02 /<my file path>/github/gorm-playground/db.go:69
[error] failed to initialize database, got error mssql: login error: Login failed for user 'gorm'.
2022/12/10 15:05:02 failed to connect database, got error mssql: login error: Login failed for user 'gorm'.
FAIL gorm.io/playground 0.185s
FAIL
2.
When running any test (by clicking on the "run test" link in vs code) - vs code returned:
go: github.com/google/uuid@v1.3.0: missing go.sum entry; to add it:
go mod download github.com/google/uuid
Comment From: a631807682
https://gorm.io/docs/sql_builder.html#ToSQL