Fixed: panic on nullable value with multiple foreign key usage (#6839)

See: https://github.com/go-gorm/playground/pull/537
This commit is contained in:
M Dmitry 2024-02-19 03:42:25 +00:00 committed by GitHub
parent 8fb9a31775
commit d81ae6f701
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 1 deletions

View File

@ -74,7 +74,11 @@ func ToStringKey(values ...interface{}) string {
case uint:
results[idx] = strconv.FormatUint(uint64(v), 10)
default:
results[idx] = fmt.Sprint(reflect.Indirect(reflect.ValueOf(v)).Interface())
results[idx] = "nil"
vv := reflect.ValueOf(v)
if vv.IsValid() && !vv.IsZero() {
results[idx] = fmt.Sprint(reflect.Indirect(vv).Interface())
}
}
}

View File

@ -48,8 +48,10 @@ func TestToStringKey(t *testing.T) {
}{
{[]interface{}{"a"}, "a"},
{[]interface{}{1, 2, 3}, "1_2_3"},
{[]interface{}{1, nil, 3}, "1_nil_3"},
{[]interface{}{[]interface{}{1, 2, 3}}, "[1 2 3]"},
{[]interface{}{[]interface{}{"1", "2", "3"}}, "[1 2 3]"},
{[]interface{}{[]interface{}{"1", nil, "3"}}, "[1 <nil> 3]"},
}
for _, c := range cases {
if key := ToStringKey(c.values...); key != c.key {