From 50d0caabc2cfed6de38675d0142f99358f2efe3f Mon Sep 17 00:00:00 2001 From: Hein Date: Tue, 3 Feb 2026 08:40:11 +0200 Subject: [PATCH] =?UTF-8?q?refactor(database):=20=E2=99=BB=EF=B8=8F=20simp?= =?UTF-8?q?lify=20relation=20type=20handling=20*=20Consolidate=20related?= =?UTF-8?q?=20type=20determination=20logic=20*=20Improve=20clarity=20in=20?= =?UTF-8?q?slice=20creation=20for=20results=20*=20Enhance=20foreign=20key?= =?UTF-8?q?=20field=20name=20handling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/common/adapters/database/bun.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/pkg/common/adapters/database/bun.go b/pkg/common/adapters/database/bun.go index c64f3b4..82c2456 100644 --- a/pkg/common/adapters/database/bun.go +++ b/pkg/common/adapters/database/bun.go @@ -633,12 +633,7 @@ func checkIfRelationAlreadyLoaded(parents reflect.Value, relationName string) (r // Check if it's a pointer (has-one/belongs-to) if !relationField.IsNil() { // Already loaded! Collect all related records from all parents - var relatedType reflect.Type - if relationField.Elem().IsValid() { - relatedType = relationField.Type() - } else { - relatedType = relationField.Type() - } + relatedType := relationField.Type() allRelated := reflect.MakeSlice(reflect.SliceOf(relatedType), 0, parents.Len()) for j := 0; j < parents.Len(); j++ { p := parents.Index(j) @@ -785,7 +780,7 @@ func (b *BunSelectQuery) loadRelationLevel(ctx context.Context, parentRecords re } // Create a slice to hold the results - resultsSlice := reflect.MakeSlice(reflect.SliceOf(reflect.PtrTo(relatedType)), 0, len(fkValues)) + resultsSlice := reflect.MakeSlice(reflect.SliceOf(reflect.PointerTo(relatedType)), 0, len(fkValues)) resultsPtr := reflect.New(resultsSlice.Type()) resultsPtr.Elem().Set(resultsSlice) @@ -876,7 +871,7 @@ func extractForeignKeyValues(records reflect.Value, fkFieldName string) ([]inter fkField := record.FieldByName(fkFieldName) if !fkField.IsValid() { // Try capitalized version - fkField = record.FieldByName(strings.Title(fkFieldName)) + fkField = record.FieldByName(strings.ToUpper(fkFieldName[:1]) + fkFieldName[1:]) } if !fkField.IsValid() { // Try finding by json tag