Skip to content

Commit

Permalink
Merge pull request #10 from sgacode/master
Browse files Browse the repository at this point in the history
Add table name to error message in the CrudRepository.CreateOrUpdateMany
  • Loading branch information
mnvx authored Nov 19, 2024
2 parents 93d7daf + 2469932 commit 7052274
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions crud_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,16 @@ func (c CrudRepository) CreateOrUpdateMany(
return nil
}

tableName := c.Db.NewScope(item).TableName()

var valueStrings []string
for _, valueMap := range values {
var valueRowString []string
for _, column := range columns {

colVal, ok := valueMap[column]
if !ok {
return errors.New(fmt.Sprintf("CreateOrUpdateMany: value for column %s found", column))
return errors.New(fmt.Sprintf("CreateOrUpdateMany: value for column %s found, table: %s", column, tableName))
}

// stringify column value
Expand Down Expand Up @@ -367,15 +369,15 @@ func (c CrudRepository) CreateOrUpdateMany(
}

query := fmt.Sprintf("INSERT INTO %s (%s) VALUES %s %s",
c.Db.NewScope(item).TableName(),
tableName,
strings.Join(columns, ","),
strings.Join(valueStrings, ","),
onConflict)

err := c.Db.Exec(query).Error
err = NormalizeErr(err)
if nil != err {
c.Logger.Errorf("gorm-crud: Error in the CreateOrUpdateMany(): %v", err)
c.Logger.Errorf("gorm-crud: Error in the CreateOrUpdateMany(): %v, table: %s", err, tableName)
}

return err
Expand Down

0 comments on commit 7052274

Please sign in to comment.