From 4b91437c88adb049a993bd63bb87b82e82c36004 Mon Sep 17 00:00:00 2001 From: Evan Owen Date: Mon, 2 Nov 2020 19:04:55 -0800 Subject: [PATCH] Treat partially zero compound foreign keys as an unset association Currently when one value of a compound foreign key is null or zero, preloading this association yields a panic ("reflect: call of reflect.Value.Interface on zero Value") because we're attempting to look up the identity values when any one of the columns is non-zero. The fix here is to require each of the values to be non-zero or we assume that the association is missing. This is not a very common scenario to have in practice, but it's an edge case that's easy to fix and resolves a use-case for me. --- schema/utils.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/schema/utils.go b/schema/utils.go index 55cbdeb4..227678c6 100644 --- a/schema/utils.go +++ b/schema/utils.go @@ -109,9 +109,10 @@ func GetIdentityFieldValuesMap(reflectValue reflect.Value, fields []*Field) (map case reflect.Struct: results = [][]interface{}{make([]interface{}, len(fields))} + notZero = true for idx, field := range fields { results[0][idx], zero = field.ValueOf(reflectValue) - notZero = notZero || !zero + notZero = notZero && !zero } if !notZero { @@ -133,10 +134,11 @@ func GetIdentityFieldValuesMap(reflectValue reflect.Value, fields []*Field) (map loaded[elemKey] = true fieldValues := make([]interface{}, len(fields)) - notZero = false + + notZero = true for idx, field := range fields { fieldValues[idx], zero = field.ValueOf(elem) - notZero = notZero || !zero + notZero = notZero && !zero } if notZero {