diff --git a/pkg/common/recursive_crud.go b/pkg/common/recursive_crud.go index 461293a..211c93a 100644 --- a/pkg/common/recursive_crud.go +++ b/pkg/common/recursive_crud.go @@ -471,13 +471,17 @@ func (p *NestedCUDProcessor) processChildRelations( // Priority: Use foreign key field name if specified var foreignKeyFieldName string if relInfo.ForeignKey != "" { - // Get the JSON name for the foreign key field in the child model - foreignKeyFieldName = reflection.GetJSONNameForField(relatedModelType, relInfo.ForeignKey) - if foreignKeyFieldName == "" { - // Fallback to lowercase field name - foreignKeyFieldName = strings.ToLower(relInfo.ForeignKey) + // For has-many/has-one: join:parentCol=childCol + // ForeignKey = parent side, References = child side (where we actually set the value) + childField := relInfo.ForeignKey + if (relInfo.RelationType == "hasMany" || relInfo.RelationType == "hasOne") && relInfo.References != "" { + childField = relInfo.References } - logger.Debug("Using foreign key field for direct assignment: %s (from FK %s)", foreignKeyFieldName, relInfo.ForeignKey) + foreignKeyFieldName = reflection.GetJSONNameForField(relatedModelType, childField) + if foreignKeyFieldName == "" { + foreignKeyFieldName = strings.ToLower(childField) + } + logger.Debug("Using foreign key field for direct assignment: %s (from FK %s -> child %s)", foreignKeyFieldName, relInfo.ForeignKey, childField) } // Get the primary key name for the child model to avoid overwriting it in recursive relationships diff --git a/pkg/restheadspec/handler.go b/pkg/restheadspec/handler.go index d734698..22c22e7 100644 --- a/pkg/restheadspec/handler.go +++ b/pkg/restheadspec/handler.go @@ -2011,11 +2011,15 @@ func (h *Handler) processChildRelationsForField( // Priority: Use foreign key field name if specified, otherwise use parent's PK name var foreignKeyFieldName string if relInfo.ForeignKey != "" { - // Get the JSON name for the foreign key field in the child model - foreignKeyFieldName = reflection.GetJSONNameForField(relatedModelType, relInfo.ForeignKey) + // For has-many/has-one: join:parentCol=childCol + // ForeignKey = parent side, References = child side (where we actually set the value) + childField := relInfo.ForeignKey + if (relInfo.RelationType == "hasMany" || relInfo.RelationType == "hasOne") && relInfo.References != "" { + childField = relInfo.References + } + foreignKeyFieldName = reflection.GetJSONNameForField(relatedModelType, childField) if foreignKeyFieldName == "" { - // Fallback to lowercase field name - foreignKeyFieldName = strings.ToLower(relInfo.ForeignKey) + foreignKeyFieldName = strings.ToLower(childField) } } else { // Fallback: use parent's primary key name