Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion callbacks/preload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
52 changes: 52 additions & 0 deletions tests/preload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down