-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Open
Labels
type:feature_requestfeature requestfeature request
Description
Describe the feature
Make Where()
treat custom string types as SQL conditions like plain strings.
Motivation
Passing a value whose underlying type is string
to Where
causes GORM to treat it as a struct/model instead of a raw SQL snippet. The dry-run output shows the condition being rewritten to `some_table`. = "col1 IS NOT NULL"
followed by the runtime error model value required
.
package not_a_string_type_test
import (
"context"
"fmt"
"testing"
"gorm.io/gorm"
)
type notString string
func (ns notString) String() string {
return string(ns)
}
const (
col1 notString = "col1"
)
func Test_not_a_string_type(t *testing.T) {
db := GetDB(context.Background()).
Session(&gorm.Session{DryRun: true})
var val map[string]any
notStrCond := col1 + " IS NOT NULL"
err := db.Table("some_table").
Where(notStrCond).
Find(&val).
Error
if err != nil {
t.Error(err)
}
// [0.030ms] [rows:0] SELECT * FROM `some_table` WHERE `some_table`. = "col1 IS NOT NULL"
// not_a_string_type_test.go:35: model value required
strCond := col1.String() + " IS NOT NULL"
err = db.Table("some_table").
Where(strCond).
Find(&val).
Error
if err != nil {
t.Fatal(err)
}
// [0.006ms] [rows:0] SELECT * FROM `some_table` WHERE col1 IS NOT NULL
}
I’m happy to contribute a PR for this change if it sounds reasonable.
Related Issues
nil.
Metadata
Metadata
Assignees
Labels
type:feature_requestfeature requestfeature request