From 7abd781f457ee57f47c6f62de9e3d203e2b57b72 Mon Sep 17 00:00:00 2001 From: kaisawind Date: Sat, 2 Apr 2022 00:14:09 +0800 Subject: [PATCH] fix: "hasMany" only insert one row(#5220) when there's no primary keys, we should append elems --- callbacks/associations.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/callbacks/associations.go b/callbacks/associations.go index fd3141cf..2107a06a 100644 --- a/callbacks/associations.go +++ b/callbacks/associations.go @@ -206,8 +206,8 @@ func SaveAfterAssociations(create bool) func(db *gorm.DB) { } } - cacheKey := utils.ToStringKey(relPrimaryValues) - if len(relPrimaryValues) != len(rel.FieldSchema.PrimaryFields) || !identityMap[cacheKey] { + cacheKey := utils.ToStringKey(relPrimaryValues...) + if len(relPrimaryValues) == 0 || len(relPrimaryValues) != len(rel.FieldSchema.PrimaryFields) || !identityMap[cacheKey] { identityMap[cacheKey] = true if isPtr { elems = reflect.Append(elems, elem)