go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
37.02k stars 3.94k forks source link

Reflection issue when trying to Join relationships #7244

Open fhendrikx opened 1 month ago

fhendrikx commented 1 month ago

GORM Playground Link

https://github.com/go-gorm/playground/pull/766

Description

I have a simple example here that shows when using First() I can preload the Pets and Pets.Toy relationship for a User, however, when trying to use the DB.Model and Rows() function to load multiple Users, this fails with the error:

panic: reflect: call of reflect.Value.Field on slice Value

goroutine 1 [running]:
reflect.Value.Field({0xafa4c0?, 0x4000612430?, 0xbe41c0?}, 0xaf6e00?)
    /usr/lib/go-1.19/src/reflect/value.go:1266 +0xec
gorm.io/gorm/schema.(*Field).setupValuerAndSetter.func5({0xbe41c0?, 0x4000612340?}, {0xafa4c0?, 0x4000612430?, 0x400053f1e8?})
    /home/ferry/go/pkg/mod/gorm.io/gorm@v1.25.4/schema/field.go:502 +0xb8
gorm.io/gorm/schema.(*Field).setupValuerAndSetter.func9({0xd21130, 0x400013a040}, {0xafa4c0?, 0x4000612430?, 0x400053f278?}, {0xae4780?, 0x40001285e8?})
    /home/ferry/go/pkg/mod/gorm.io/gorm@v1.25.4/schema/field.go:689 +0x2fc
gorm.io/gorm.(*DB).scanIntoStruct(0x40004e9410, {0xd227b8, 0x40004eaa80}, {0xbe41c0?, 0x4000612340?, 0x4549f4?}, {0x400017f500?, 0x17, 0x17}, {0x40004ce480, ...}, ...)
    /home/ferry/go/pkg/mod/gorm.io/gorm@v1.25.4/scan.go:105 +0x434
gorm.io/gorm.Scan({0xd227b8, 0x40004eaa80}, 0x40004e9410, 0x1)
    /home/ferry/go/pkg/mod/gorm.io/gorm@v1.25.4/scan.go:328 +0xbf4
gorm.io/gorm.(*DB).ScanRows(0x40004eaa80?, 0x400?, {0xaf4240?, 0x4000612340})
    /home/ferry/go/pkg/mod/gorm.io/gorm@v1.25.4/finisher_api.go:586 +0x2e0
main.main()
    /home/ferry/dev/playground/main.go:63 +0x534
exit status 2

I apologies in advance if I have misunderstood something about GORM.

OS: Debian 12 GORM: 1.25.4 DB: Postgres 15

Thanks, Ferry

tboerger commented 1 month ago

Looks like I'm running into the same issue... reflect: call of reflect.Value.Field on slice Value

db.WithContext(
    r.Context(),
).Model(
    &model.User{},
).InnerJoins(
    "Auths",
    db.Where(&model.UserAuth{
        Provider: "scim",
    }),
).Order(
    "username ASC",
).Find(
    &records,
)
fhendrikx commented 3 weeks ago

Hi @tboerger Any thoughts on efficient work-arounds?

tboerger commented 3 weeks ago

I haven't found any working solution based on Gorm. Since nobody is really responding to this issue I have simply switched away from Form for my small project to Bun.

fhendrikx commented 3 weeks ago

Thanks, I'm considering simply going back to using sqlx.