Don't overwrite primary key if already it is already exist

This commit is contained in:
Jinzhu 2015-06-01 11:04:11 +08:00
parent b96ca76e59
commit 331d8ceabd
2 changed files with 13 additions and 1 deletions

View File

@ -70,7 +70,7 @@ func Create(scope *Scope) {
id, err := result.LastInsertId() id, err := result.LastInsertId()
if scope.Err(err) == nil { if scope.Err(err) == nil {
scope.db.RowsAffected, _ = result.RowsAffected() scope.db.RowsAffected, _ = result.RowsAffected()
if primaryField != nil { if primaryField != nil && primaryField.IsBlank {
scope.Err(scope.SetColumn(primaryField, id)) scope.Err(scope.SetColumn(primaryField, id))
} }
} }

View File

@ -61,6 +61,18 @@ func init() {
runMigration() 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) { func TestExceptionsWithInvalidSql(t *testing.T) {
var columns []string var columns []string
if DB.Where("sdsd.zaaa = ?", "sd;;;aa").Pluck("aaa", &columns).Error == nil { if DB.Where("sdsd.zaaa = ?", "sd;;;aa").Pluck("aaa", &columns).Error == nil {