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

Error scanning null values into models when using null.String #7234

Open henryjcee opened 1 month ago

henryjcee commented 1 month ago

Playground link

https://github.com/go-gorm/playground/pull/762/files

Note I've made a change in this that assumes you have gorm.io/gen checked out to fix the current issue with Gorm playground not running ootb. See here for details.

Description

We are using Gurego null as an optional type. I've noticed an issue when using DB.Model() to read values that isn't correctly reading null values into a struct. I think the issue was caused by this change to embedded struct handling.

What appears to be the issue is that under the new logic:

I think what needs to happen is that if the struct field is a non-pointer type (and the column is null), the struct field should be set to the zero value, not skipped (as is happening now). I assume this'll have implications for embedded field handling (i.e. if it's not happening already, a struct embedded via a pointer, we'll need to initialise it if any of the corresponding columns in the row are non-null) but I'm running out of time to spend on this. That should be enough to go on but let me know if more would be useful.