mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-01-18 00:44:25 +00:00
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.
This commit is contained in:
@@ -207,9 +207,9 @@ func (p *NestedCUDProcessor) processInsert(
|
|||||||
for key, value := range data {
|
for key, value := range data {
|
||||||
query = query.Value(key, value)
|
query = query.Value(key, value)
|
||||||
}
|
}
|
||||||
|
pkName := reflection.GetPrimaryKeyName(tableName)
|
||||||
// Add RETURNING clause to get the inserted ID
|
// Add RETURNING clause to get the inserted ID
|
||||||
query = query.Returning("id")
|
query = query.Returning(pkName)
|
||||||
|
|
||||||
result, err := query.Exec(ctx)
|
result, err := query.Exec(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -220,8 +220,8 @@ func (p *NestedCUDProcessor) processInsert(
|
|||||||
var id interface{}
|
var id interface{}
|
||||||
if lastID, err := result.LastInsertId(); err == nil && lastID > 0 {
|
if lastID, err := result.LastInsertId(); err == nil && lastID > 0 {
|
||||||
id = lastID
|
id = lastID
|
||||||
} else if data["id"] != nil {
|
} else if data[pkName] != nil {
|
||||||
id = data["id"]
|
id = data[pkName]
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Debug("Insert successful, ID: %v, rows affected: %d", id, result.RowsAffected())
|
logger.Debug("Insert successful, ID: %v, rows affected: %d", id, result.RowsAffected())
|
||||||
|
|||||||
Reference in New Issue
Block a user