forked from mirror/gorm
Fixes scan when primary key is not defined in gorm (but is defined db-size). Useful in join tables
This commit is contained in:
parent
21f4de584f
commit
907ea93b43
|
@ -35,9 +35,7 @@ func Create(scope *Scope) {
|
||||||
}
|
}
|
||||||
|
|
||||||
returningKey := "*"
|
returningKey := "*"
|
||||||
if scope.PrimaryKey() == "" {
|
if scope.PrimaryKey() != "" {
|
||||||
returningKey = "*"
|
|
||||||
} else {
|
|
||||||
returningKey = scope.PrimaryKey()
|
returningKey = scope.PrimaryKey()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,13 +65,25 @@ func Create(scope *Scope) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if scope.PrimaryKey() == "" {
|
||||||
|
if rows, err := scope.DB().Query(scope.Sql, scope.SqlVars...); err != nil {
|
||||||
|
//extract column name to get fields lenght
|
||||||
|
if names, columnsErr := rows.Columns(); columnsErr != nil {
|
||||||
|
ids := make([]interface{}, len(names))
|
||||||
|
if scope.Err(rows.Scan(ids...)) == nil {
|
||||||
|
scope.db.RowsAffected = int64(len(names))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if scope.Err(scope.DB().QueryRow(scope.Sql, scope.SqlVars...).Scan(&id)) == nil {
|
if scope.Err(scope.DB().QueryRow(scope.Sql, scope.SqlVars...).Scan(&id)) == nil {
|
||||||
scope.db.RowsAffected = 1
|
scope.db.RowsAffected = 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if !scope.HasError() && scope.PrimaryKeyZero() {
|
if scope.PrimaryKey() != "" && !scope.HasError() && scope.PrimaryKeyZero() {
|
||||||
scope.SetColumn(scope.PrimaryKey(), id)
|
scope.SetColumn(scope.PrimaryKey(), id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue