Add test case for pointer join with nested prelaods

This commit is contained in:
Nico Schäfer 2024-05-02 10:32:47 +00:00 committed by GitHub
parent 2aeabe29e3
commit 076c3c2c1b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -374,6 +374,58 @@ func TestNestedPreloadWithNestedJoin(t *testing.T) {
AssertEqual(t, finds[0], value) AssertEqual(t, finds[0], value)
} }
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) { func TestEmbedPreload(t *testing.T) {
type Country struct { type Country struct {
ID int `gorm:"primaryKey"` ID int `gorm:"primaryKey"`