Skip to content

Commit a7e640a

Browse files
author
Hein
committed
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.
1 parent bf7125e commit a7e640a

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

pkg/common/recursive_crud.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,9 @@ func (p *NestedCUDProcessor) processInsert(
207207
for key, value := range data {
208208
query = query.Value(key, value)
209209
}
210-
210+
pkName := reflection.GetPrimaryKeyName(tableName)
211211
// Add RETURNING clause to get the inserted ID
212-
query = query.Returning("id")
212+
query = query.Returning(pkName)
213213

214214
result, err := query.Exec(ctx)
215215
if err != nil {
@@ -220,8 +220,8 @@ func (p *NestedCUDProcessor) processInsert(
220220
var id interface{}
221221
if lastID, err := result.LastInsertId(); err == nil && lastID > 0 {
222222
id = lastID
223-
} else if data["id"] != nil {
224-
id = data["id"]
223+
} else if data[pkName] != nil {
224+
id = data[pkName]
225225
}
226226

227227
logger.Debug("Insert successful, ID: %v, rows affected: %d", id, result.RowsAffected())

0 commit comments

Comments
 (0)