Fix scanIntoStruct (#5241)

* Reproduces error case

* Fix scanIntoStruct

Co-authored-by: Filippo Del Moro <filippo.delmoro@facile.it>
This commit is contained in:
Filippo Del Moro 2022-04-13 09:47:04 +02:00 committed by GitHub
parent 74e07b049c
commit 6aa6d37fc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 3 deletions

View File

@ -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()))

View File

@ -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)
}