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

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

Closed
wants to merge 8 commits into from

Conversation

henryjcee
Copy link

  • Do only one thing
  • Non breaking API changes
  • [?] Tested

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 henryjcee changed the title Null fields are ignored when scanning to a model struct WIP - Null fields are ignored when scanning to a model struct Oct 10, 2024
@henryjcee
Copy link
Author

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

@henryjcee henryjcee closed this Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant