Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unsupported data on nested joins with preloads #6957

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion callbacks/preload.go
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
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