gorm
gorm copied to clipboard
ScanRows doesn't reset fields in target struct for incoming null values
GORM Playground Link
https://github.com/go-gorm/playground/pull/686
Description
Starting with gorm v1.25.1 (v1.25.0 was not affected), if I use ScanRows on a struct that is not initialized to its default value for all fields GORM will not overwrite those fields that are NULL on SQL side.
An example codpiece can be found at https://github.com/go-gorm/playground/pull/686, but generally speaking we have encountered it when using a non-builtin type too (https://github.com/shopspring/decimal).
I have opened a PR with some fixes: https://github.com/go-gorm/gorm/pull/6837/files
I just ran into this issue as well when upgrading gorm. It breaks the example from the docs where you initialize the var once:
var datasetRow T
for rows.Next() {
err := db.ScanRows(rows, &datasetRow)
now I need to reallocate for each scan
for rows.Next() {
var datasetRow T
err := db.ScanRows(rows, &datasetRow)
Actually this is broken since v1.24.6 (time.Time scanning NULL values still have the same error), I will be downgrading to 1.24.5 in the meantime.