gorm icon indicating copy to clipboard operation
gorm copied to clipboard

ScanRows doesn't reset fields in target struct for incoming null values

Open turip opened this issue 5 months ago • 3 comments

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).

turip avatar Feb 02 '24 13:02 turip

I have opened a PR with some fixes: https://github.com/go-gorm/gorm/pull/6837/files

turip avatar Feb 13 '24 18:02 turip

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)

Waldeedle avatar Apr 03 '24 13:04 Waldeedle

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.

Waldeedle avatar Apr 03 '24 22:04 Waldeedle