forked from mirror/gorm
Don't overwrite primary key if already it is already exist
This commit is contained in:
parent
b96ca76e59
commit
331d8ceabd
|
@ -70,7 +70,7 @@ func Create(scope *Scope) {
|
|||
id, err := result.LastInsertId()
|
||||
if scope.Err(err) == nil {
|
||||
scope.db.RowsAffected, _ = result.RowsAffected()
|
||||
if primaryField != nil {
|
||||
if primaryField != nil && primaryField.IsBlank {
|
||||
scope.Err(scope.SetColumn(primaryField, id))
|
||||
}
|
||||
}
|
||||
|
|
12
main_test.go
12
main_test.go
|
@ -61,6 +61,18 @@ func init() {
|
|||
runMigration()
|
||||
}
|
||||
|
||||
func TestStringPrimaryKey(t *testing.T) {
|
||||
type UUIDStruct struct {
|
||||
ID string `gorm:"primary_key"`
|
||||
Name string
|
||||
}
|
||||
|
||||
data := UUIDStruct{ID: "uuid", Name: "hello"}
|
||||
if err := DB.Save(&data).Error; err != nil || data.ID != "uuid" {
|
||||
t.Errorf("string primary key should not be populated")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExceptionsWithInvalidSql(t *testing.T) {
|
||||
var columns []string
|
||||
if DB.Where("sdsd.zaaa = ?", "sd;;;aa").Pluck("aaa", &columns).Error == nil {
|
||||
|
|
Loading…
Reference in New Issue