Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
henryjcee opened this issue Oct 10, 2024 · 0 comments
Open

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

henryjcee opened this issue Oct 10, 2024 · 0 comments
Assignees
Labels
type:with reproduction steps with reproduction steps

Comments

@henryjcee
Copy link

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:

  • WHEN a null column is fetched from the database
    • AND the field type is a struct (and not a pointer)
  • IF the returned value is a nil pointer
  • DO nothing. See the early return without setting a value here

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.

@github-actions github-actions bot added the type:with reproduction steps with reproduction steps label Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:with reproduction steps with reproduction steps
Projects
None yet
Development

No branches or pull requests

2 participants