fix: `unsupported data` on nested joins with preloads (#6957)

* fix: `unsupported data` on nested joins with preloads

* Add test case for pointer join with nested prelaods

* Fix tests
This commit is contained in:
Nico Schäfer 2024-06-12 12:00:47 +02:00 committed by GitHub
parent 9c4070ed19
commit 3fe7fcf356
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 53 additions and 1 deletions

View File

@ -140,7 +140,7 @@ func preloadEntryPoint(db *gorm.DB, joins []string, relationships *schema.Relati
return err
}
}
case reflect.Struct:
case reflect.Struct, reflect.Pointer:
reflectValue := rel.Field.ReflectValueOf(db.Statement.Context, rv)
tx := preloadDB(db, reflectValue, reflectValue.Interface())
if err := preloadEntryPoint(tx, nestedJoins, &tx.Statement.Schema.Relationships, preloadMap[name], associationsConds); err != nil {

View File

@ -440,6 +440,58 @@ func TestMergeNestedPreloadWithNestedJoin(t *testing.T) {
}
}
func TestNestedPreloadWithPointerJoin(t *testing.T) {
type (
Preload struct {
ID uint
Value string
JoinID uint
}
Join struct {
ID uint
Value string
Preload Preload
NestedID uint
}
Nested struct {
ID uint
Join Join
ValueID uint
}
Value struct {
ID uint
Name string
Nested *Nested
}
)
DB.Migrator().DropTable(&Preload{}, &Join{}, &Nested{}, &Value{})
DB.Migrator().AutoMigrate(&Preload{}, &Join{}, &Nested{}, &Value{})
value := Value{
Name: "value",
Nested: &Nested{
Join: Join{
Value: "j1",
Preload: Preload{
Value: "p1",
},
},
},
}
if err := DB.Create(&value).Error; err != nil {
t.Errorf("failed to create value, got err: %v", err)
}
var find1 Value
err := DB.Table("values").Joins("Nested").Joins("Nested.Join").Preload("Nested.Join.Preload").First(&find1).Error
if err != nil {
t.Errorf("failed to find value, got err: %v", err)
}
AssertEqual(t, find1, value)
}
func TestEmbedPreload(t *testing.T) {
type Country struct {
ID int `gorm:"primaryKey"`