fix(reflection): enhance GetForeignKeyColumn logic for self-referential models

* Add support for self-referential models in GetForeignKeyColumn
* Update comments for clarity on foreign key resolution strategies
* Introduce selfRefItem struct for testing self-referential behavior
This commit is contained in:
Hein
2026-05-18 13:03:07 +02:00
parent c42fa11c1a
commit 4244e838b1
3 changed files with 71 additions and 11 deletions
+5
View File
@@ -234,6 +234,8 @@ func (p *NestedCUDProcessor) injectForeignKeys(data map[string]interface{}, mode
return
}
pkCol := reflection.GetPrimaryKeyName(reflect.New(modelType).Interface())
for parentKey, parentID := range parentIDs {
dbColNames := reflection.GetForeignKeyColumn(modelType, parentKey)
@@ -255,6 +257,9 @@ func (p *NestedCUDProcessor) injectForeignKeys(data map[string]interface{}, mode
}
for _, dbColName := range dbColNames {
if pkCol != "" && strings.EqualFold(dbColName, pkCol) {
continue
}
if _, exists := data[dbColName]; !exists {
logger.Debug("Injecting foreign key: %s = %v", dbColName, parentID)
data[dbColName] = parentID