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.
This commit is contained in:
Evan Owen 2020-11-02 19:04:55 -08:00
parent 57b033e2dd
commit 4b91437c88

View File

@ -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 {