From 6aa6d37fc47a433510ac05e2f01eb33e57d7cb6c Mon Sep 17 00:00:00 2001 From: Filippo Del Moro Date: Wed, 13 Apr 2022 09:47:04 +0200 Subject: [PATCH] Fix scanIntoStruct (#5241) * Reproduces error case * Fix scanIntoStruct Co-authored-by: Filippo Del Moro --- scan.go | 2 +- tests/joins_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/scan.go b/scan.go index 2ce6bd28..ad3734d8 100644 --- a/scan.go +++ b/scan.go @@ -74,7 +74,7 @@ func (db *DB) scanIntoStruct(rows Rows, reflectValue reflect.Value, values []int relValue := joinFields[idx][0].ReflectValueOf(db.Statement.Context, reflectValue) if relValue.Kind() == reflect.Ptr && relValue.IsNil() { if value := reflect.ValueOf(values[idx]).Elem(); value.Kind() == reflect.Ptr && value.IsNil() { - return + continue } relValue.Set(reflect.New(relValue.Type().Elem())) diff --git a/tests/joins_test.go b/tests/joins_test.go index bb5352ef..4908e5ba 100644 --- a/tests/joins_test.go +++ b/tests/joins_test.go @@ -10,12 +10,12 @@ import ( ) func TestJoins(t *testing.T) { - user := *GetUser("joins-1", Config{Company: true, Manager: true, Account: true}) + user := *GetUser("joins-1", Config{Company: true, Manager: true, Account: true, NamedPet: false}) DB.Create(&user) var user2 User - if err := DB.Joins("Company").Joins("Manager").Joins("Account").First(&user2, "users.name = ?", user.Name).Error; err != nil { + if err := DB.Joins("NamedPet").Joins("Company").Joins("Manager").Joins("Account").First(&user2, "users.name = ?", user.Name).Error; err != nil { t.Fatalf("Failed to load with joins, got error: %v", err) }