go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
37.05k stars 3.94k forks source link

WIP - Null fields are ignored when scanning to a model struct #7233

Closed henryjcee closed 1 month ago

henryjcee commented 1 month ago

What did this pull request do?

We're having issues reading a row into a struct with nullable fields, represented by the guregu/null.v3 types; specifically in the case where the columns in the table have been updated from non-null to null. e.g.

err := tx.Model(target).
        WithContext(ctx).
        Where(target, "ID").
        Preload(clause.Associations).
        First(target).
        Error

In this case we'd expect the struct field to be set to null.Time{} but what we find is that the field is left unchanged. I've tracked the issue down to this commit and I can't make out what the intention of the change by looking at the previous logic:

} else if reflectV.Kind() == reflect.Ptr {
    if reflectV.IsNil() || !reflectV.IsValid() {
        field.ReflectValueOf(ctx, value).Set(reflect.New(field.FieldType).Elem())
    } else {
        return field.Set(ctx, value, reflectV.Elem().Interface())
    }
}

The commit in question mentions fixing a problem related to time fields but this block looks unrelated.

henryjcee commented 1 month ago

Closing this in favour of an issue now that I understand the problem