Skip to content

Commit

Permalink
Regression with gorm version 1.25.7
Browse files Browse the repository at this point in the history
Panic Occurs when Mixing Joins and Preload
  • Loading branch information
ldjebran committed Feb 8, 2024
1 parent 4f84925 commit ce56187
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 21 deletions.
34 changes: 18 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,34 @@ module gorm.io/playground
go 1.20

require (
gorm.io/driver/mysql v1.5.2
gorm.io/driver/postgres v1.5.2
gorm.io/driver/sqlite v1.5.3
gorm.io/driver/sqlserver v1.5.1
gorm.io/driver/mysql v1.5.4
gorm.io/driver/postgres v1.5.6
gorm.io/driver/sqlite v1.5.5
gorm.io/driver/sqlserver v1.5.3
gorm.io/gen v0.3.25
gorm.io/gorm v1.25.4
gorm.io/gorm v1.25.7
)

require (
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.4.3 // indirect
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
github.com/jackc/pgx/v5 v5.5.3 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/microsoft/go-mssqldb v1.5.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.15.0 // indirect
gorm.io/datatypes v1.1.1-0.20230130040222-c43177d3cf8c // indirect
gorm.io/hints v1.1.0 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/microsoft/go-mssqldb v1.6.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/mod v0.15.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.17.0 // indirect
gorm.io/datatypes v1.2.0 // indirect
gorm.io/hints v1.1.2 // indirect
gorm.io/plugin/dbresolver v1.5.0 // indirect
)

Expand Down
81 changes: 76 additions & 5 deletions main_test.go
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)
}
})
}
}

0 comments on commit ce56187

Please sign in to comment.