go-gorm / gorm

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

Join Preload panics #7013

Open emilienkofman opened 1 month ago

emilienkofman commented 1 month ago

GORM Playground Link

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

Description

When using Join and Preload together, if we get more than 20 entries in the database it will panic (I guess due to this beeing 20)

gorm version 1.25.10

obtained stack trace:

Error Trace:    /home/emilien/Workspace/src/github.com/emilienkofman/playground/main_test.go:34
            Error:          func (assert.PanicTestFunc)(0xc9e8a0) should not panic
                                Panic value:    reflect: slice length out of range in SetLen
                                Panic stack:    goroutine 24 [running]:
                            runtime/debug.Stack()
                                /home/emilien/Workspace/go/src/runtime/debug/stack.go:24 +0x6b
                            github.com/stretchr/testify/assert.didPanic.func1()
                                /home/emilien/go/pkg/mod/github.com/stretchr/testify@v1.8.1/assert/assertions.go:1050 +0x73
                            panic({0xdf8380?, 0xf6ffd0?})
                                /home/emilien/Workspace/go/src/runtime/panic.go:770 +0x136
                            reflect.Value.SetLen({0xc0003fb100, 0xc00044f068, 0x197}, 0x15)
                                /home/emilien/Workspace/go/src/reflect/value.go:2423 +0x85
                            gorm.io/gorm/callbacks.preloadEntryPoint(0xc000488c00, {0xc0003dfeb0, 0x1, 0x1}, 0xc000444a28, 0xc000488660, {0x0, 0x0, 0x0})
                                /home/emilien/Workspace/src/github.com/emilienkofman/playground/gorm/callbacks/preload.go:128 +0x745
                            gorm.io/gorm/callbacks.Preload(0xc000488600)
                                /home/emilien/Workspace/src/github.com/emilienkofman/playground/gorm/callbacks/query.go:283 +0x4f7
                            gorm.io/gorm.(*processor).Execute(0xc0003feaf0, 0xc000488600)
                                /home/emilien/Workspace/src/github.com/emilienkofman/playground/gorm/callbacks.go:130 +0x67c
                            gorm.io/gorm.(*DB).Find(0xc000488600, {0xde9280, 0xc00044e258}, {0x0, 0x0, 0x0})
                                /home/emilien/Workspace/src/github.com/emilienkofman/playground/gorm/finisher_api.go:170 +0x285
                            gorm.io/playground.TestGORM.func1()
                                /home/emilien/Workspace/src/github.com/emilienkofman/playground/main_test.go:38 +0xac
                            github.com/stretchr/testify/assert.didPanic(0xc0003de590)
                                /home/emilien/go/pkg/mod/github.com/stretchr/testify@v1.8.1/assert/assertions.go:1055 +0x119
                            github.com/stretchr/testify/assert.NotPanics({0xf71740, 0xc0000f51e0}, 0xc0003de590, {0x0, 0x0, 0x0})
                                /home/emilien/go/pkg/mod/github.com/stretchr/testify@v1.8.1/assert/assertions.go:1126 +0x12c
                            gorm.io/playground.TestGORM(0xc0000f51e0)
                                /home/emilien/Workspace/src/github.com/emilienkofman/playground/main_test.go:34 +0x459
                            testing.tRunner(0xc0000f51e0, 0xf26bb0)
                                /home/emilien/Workspace/go/src/testing/testing.go:1689 +0x1da
                            created by testing.(*T).Run in goroutine 1
                                /home/emilien/Workspace/go/src/testing/testing.go:1742 +0x7d3

the test case is inspired with https://github.com/go-gorm/gorm/issues/6715 and likely related to the fix of that previous issue (but I've not been investigating enough)

https://github.com/go-gorm/gorm/issues/6715

emilienkofman commented 1 month ago

looks like this was introduced in https://github.com/go-gorm/gorm/commit/85299bfca7172489d7f93a5525ee5ab0d92d514b

@a631807682 do you have any insight on how to fix that? Let me know if I can help. I'm not sure exactly what to do at the moment.

a631807682 commented 1 month ago

looks like this was introduced in 85299bf

@a631807682 do you have any insight on how to fix that? Let me know if I can help. I'm not sure exactly what to do at the moment.

Cap and Len are not handled correctly here, we need to handle them correctly, or avoid them through Append. Are you interested to create a PR for it?