From 141954265009fdea2b9b59e57576ae14929e7f7e Mon Sep 17 00:00:00 2001 From: Hein Date: Fri, 12 Jun 2026 13:37:07 +0200 Subject: [PATCH] fix(handler): re-fetch records to capture DB-generated changes --- pkg/resolvemcp/handler.go | 38 ++++++++++++++++++++++++++++++++++++ pkg/websocketspec/handler.go | 6 ++++++ 2 files changed, 44 insertions(+) diff --git a/pkg/resolvemcp/handler.go b/pkg/resolvemcp/handler.go index 53e5936..5d3f53b 100644 --- a/pkg/resolvemcp/handler.go +++ b/pkg/resolvemcp/handler.go @@ -428,6 +428,8 @@ func (h *Handler) executeCreate(ctx context.Context, schema, entity string, data // Use potentially modified data data = hookCtx.Data + pkName := reflection.GetPrimaryKeyName(model) + switch v := data.(type) { case map[string]interface{}: query := h.db.NewInsert().Table(tableName) @@ -437,6 +439,23 @@ func (h *Handler) executeCreate(ctx context.Context, schema, entity string, data if _, err := query.Exec(ctx); err != nil { return nil, fmt.Errorf("create error: %w", err) } + // Re-fetch after insert to capture DB-generated defaults/triggers. + if pkVal, ok := v[pkName]; ok && pkVal != nil { + modelType := reflect.TypeOf(model) + if modelType.Kind() == reflect.Ptr { + modelType = modelType.Elem() + } + fetchedRecord := reflect.New(modelType).Interface() + if err := h.db.NewSelect().Model(fetchedRecord). + Where(fmt.Sprintf("%s = ?", common.QuoteIdent(pkName)), pkVal). + ScanModel(ctx); err == nil { + jsonData, _ := json.Marshal(fetchedRecord) + var fetchedMap map[string]interface{} + if json.Unmarshal(jsonData, &fetchedMap) == nil { + v = fetchedMap + } + } + } hookCtx.Result = v if err := h.hooks.Execute(AfterCreate, hookCtx); err != nil { return nil, fmt.Errorf("AfterCreate hook failed: %w", err) @@ -584,6 +603,25 @@ func (h *Handler) executeUpdate(ctx context.Context, schema, entity, id string, if err != nil { return nil, err } + + // Re-fetch the record after transaction commits to capture DB-generated changes. + modelType := reflect.TypeOf(model) + if modelType.Kind() == reflect.Ptr { + modelType = modelType.Elem() + } + fetchedRecord := reflect.New(modelType).Interface() + if err := h.db.NewSelect().Model(fetchedRecord). + Where(fmt.Sprintf("%s = ?", common.QuoteIdent(pkName)), id). + ScanModel(ctx); err == nil { + jsonData, marshalErr := json.Marshal(fetchedRecord) + if marshalErr == nil { + var fetchedMap map[string]interface{} + if json.Unmarshal(jsonData, &fetchedMap) == nil { + updateResult = fetchedMap + } + } + } + return updateResult, nil } diff --git a/pkg/websocketspec/handler.go b/pkg/websocketspec/handler.go index e3881d8..317410f 100644 --- a/pkg/websocketspec/handler.go +++ b/pkg/websocketspec/handler.go @@ -671,6 +671,12 @@ func (h *Handler) create(hookCtx *HookContext) (interface{}, error) { return nil, fmt.Errorf("failed to create record: %w", err) } + // Re-fetch the created record to capture DB-generated defaults/triggers. + if pkVal := reflection.GetPrimaryKeyValue(hookCtx.ModelPtr); pkVal != nil { + hookCtx.ID = fmt.Sprintf("%v", pkVal) + return h.readByID(hookCtx) + } + return hookCtx.ModelPtr, nil }