mirror of https://github.com/go-gorm/gorm.git
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()
|
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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
12
main_test.go
12
main_test.go
|
@ -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 {
|
||||||
|
|
Loading…
Reference in New Issue