handle string primary fields

This commit is contained in:
Jim Lambert 2020-02-13 21:31:49 -05:00
parent 100e21cb36
commit ce1b20789c

View File

@ -3,6 +3,7 @@ package gorm
import ( import (
"database/sql" "database/sql"
"fmt" "fmt"
"reflect"
"strings" "strings"
) )
@ -146,12 +147,27 @@ func createCallback(scope *Scope) {
} }
if scope.IsOracle() { if scope.IsOracle() {
var id uint32 var stringId string
scope.SQLVars = append(scope.SQLVars, sql.Out{Dest: &id}) var intId uint32
scope.SQL = fmt.Sprintf("%s returning id into :%d", scope.SQL, len(scope.SQLVars)) primaryIsString := false
out := sql.Out{
Dest: &intId,
}
if primaryField.Field.Kind() == reflect.String {
out = sql.Out{
Dest: &stringId,
}
primaryIsString = true
}
scope.SQLVars = append(scope.SQLVars, out)
scope.SQL = fmt.Sprintf("%s returning %s into :%d", scope.SQL, scope.Quote(primaryField.DBName), len(scope.SQLVars))
if result, err := scope.SQLDB().Exec(scope.SQL, scope.SQLVars...); scope.Err(err) == nil { if result, err := scope.SQLDB().Exec(scope.SQL, scope.SQLVars...); scope.Err(err) == nil {
scope.db.RowsAffected, _ = result.RowsAffected() scope.db.RowsAffected, _ = result.RowsAffected()
scope.Err(primaryField.Set(id)) if primaryIsString {
scope.Err(primaryField.Set(stringId))
} else {
scope.Err(primaryField.Set(intId))
}
} }
return return
} }