Compare commits

...

1 Commits

Author SHA1 Message Date
Hein
a7e640a6a1 fix(recursive_crud): 🐛 use dynamic primary key name in insert
* Update processInsert to use the primary key name dynamically.
* Ensure correct ID retrieval from data based on primary key.
2026-01-07 11:58:44 +02:00

View File

@@ -207,9 +207,9 @@ func (p *NestedCUDProcessor) processInsert(
for key, value := range data {
query = query.Value(key, value)
}
pkName := reflection.GetPrimaryKeyName(tableName)
// Add RETURNING clause to get the inserted ID
query = query.Returning("id")
query = query.Returning(pkName)
result, err := query.Exec(ctx)
if err != nil {
@@ -220,8 +220,8 @@ func (p *NestedCUDProcessor) processInsert(
var id interface{}
if lastID, err := result.LastInsertId(); err == nil && lastID > 0 {
id = lastID
} else if data["id"] != nil {
id = data["id"]
} else if data[pkName] != nil {
id = data[pkName]
}
logger.Debug("Insert successful, ID: %v, rows affected: %d", id, result.RowsAffected())