-
-
Notifications
You must be signed in to change notification settings - Fork 699
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Panic Occurs when Mixing Joins and Preload
- Loading branch information
Showing
2 changed files
with
94 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,91 @@ | ||
package main | ||
|
||
import ( | ||
"gorm.io/gorm" | ||
"testing" | ||
) | ||
|
||
// GORM_REPO: https://github.com/go-gorm/gorm.git | ||
// GORM_BRANCH: master | ||
// TEST_DRIVERS: sqlite, mysql, postgres, sqlserver | ||
|
||
type Repo struct { | ||
ID uint `gorm:"primarykey"` | ||
URL string `json:"RepoURL"` | ||
Status string `json:"RepoStatus"` | ||
} | ||
|
||
type DispatchRecord struct { | ||
ID uint `gorm:"primarykey"` | ||
PlaybookURL string | ||
PlaybookDispatcherID string | ||
} | ||
|
||
type UpdateTransaction struct { | ||
ID uint `gorm:"primarykey"` | ||
RepoID *uint | ||
Repo *Repo | ||
DispatchRecords []DispatchRecord `gorm:"many2many:updatetransaction_dispatchrecords"` | ||
} | ||
|
||
type DeviceUpdate struct { | ||
ID uint `gorm:"primarykey"` | ||
Name string | ||
UpdateID *uint | ||
Update *UpdateTransaction | ||
} | ||
|
||
func TestGORM(t *testing.T) { | ||
user := User{Name: "jinzhu"} | ||
_ = DB.AutoMigrate(&Repo{}) | ||
_ = DB.AutoMigrate(&DispatchRecord{}) | ||
_ = DB.AutoMigrate(&UpdateTransaction{}) | ||
_ = DB.AutoMigrate(&DeviceUpdate{}) | ||
|
||
deviceUpdate := DeviceUpdate{ | ||
Name: "a devioce update", | ||
// Device: &Device{UUID: faker.UUIDHyphenated(), OrgID: orgID}, | ||
Update: &UpdateTransaction{Repo: &Repo{URL: "http://some.example.com/content"}}, | ||
} | ||
DB.Create(&deviceUpdate) | ||
|
||
var deviceUpdates []DeviceUpdate | ||
|
||
DB.Create(&user) | ||
testCases := []struct { | ||
Name string | ||
Query *gorm.DB | ||
}{ | ||
{ | ||
Name: "2 preloads", | ||
Query: DB.Preload("Update.Repo").Preload("Update").Find(&deviceUpdates), | ||
}, | ||
{ | ||
Name: "2 Joins", | ||
Query: DB.Joins("Update.Repo").Joins("Update").Find(&deviceUpdates), | ||
}, | ||
{ | ||
Name: "3 Preloads", | ||
Query: DB.Preload("Update.Repo").Preload("Update").Preload("Update.DispatchRecords"), | ||
}, | ||
// all bellow fails with panic with gorm version 1.25.7 | ||
{ | ||
Name: "1 Preload 1 Join ", | ||
Query: DB.Preload("Update.Repo").Joins("Update").Find(&deviceUpdates), | ||
}, | ||
{ | ||
Name: "2 Preloads 1 Join ", | ||
Query: DB.Preload("Update.Repo").Joins("Update").Preload("Update.DispatchRecords"), | ||
}, | ||
{ | ||
Name: "1 Preload 2 Joins ", | ||
Query: DB.Joins("Update.Repo").Joins("Update").Preload("Update.DispatchRecords").Find(&deviceUpdates), | ||
}, | ||
} | ||
|
||
var result User | ||
if err := DB.First(&result, user.ID).Error; err != nil { | ||
t.Errorf("Failed, got error: %v", err) | ||
for _, testCase := range testCases { | ||
t.Run(testCase.Name, func(t *testing.T) { | ||
if err := testCase.Query.Find(&deviceUpdates).Error; err != nil { | ||
t.Errorf("Failed, got error: %v", err) | ||
} | ||
}) | ||
} | ||
} |