From 0fa1335555f40164b4da8c0d888e48863f18ad65 Mon Sep 17 00:00:00 2001 From: Paolo Galeone Date: Mon, 8 Dec 2014 11:33:30 +0100 Subject: [PATCH] Avoid Errors in postgres when creating a row without a GORM defined primary key (but defined db-side) --- callback_create.go | 11 +++++++++-- create_test.go | 10 +++++++++- migration_test.go | 10 +--------- structs_test.go | 6 ++++++ 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/callback_create.go b/callback_create.go index 1510122f..342fb413 100644 --- a/callback_create.go +++ b/callback_create.go @@ -34,10 +34,17 @@ func Create(scope *Scope) { } } + returningField := "" + if scope.PrimaryKey() == "" { + returningField = "*" + } else { + returningField = scope.PrimaryKey() + } + if len(columns) == 0 { scope.Raw(fmt.Sprintf("INSERT INTO %v DEFAULT VALUES %v", scope.QuotedTableName(), - scope.Dialect().ReturningStr(scope.PrimaryKey()), + scope.Dialect().ReturningStr(returningField), )) } else { scope.Raw(fmt.Sprintf( @@ -45,7 +52,7 @@ func Create(scope *Scope) { scope.QuotedTableName(), strings.Join(columns, ","), strings.Join(sqls, ","), - scope.Dialect().ReturningStr(scope.PrimaryKey()), + scope.Dialect().ReturningStr(returningField), )) } diff --git a/create_test.go b/create_test.go index f72933b4..a5b46630 100644 --- a/create_test.go +++ b/create_test.go @@ -56,10 +56,18 @@ func TestCreate(t *testing.T) { } } +func TestCreateWithNoGORMPrimayKey(t *testing.T) { + jt := JoinTable{From: 1, To: 2} + err := DB.Create(&jt).Error + if err != nil { + t.Errorf("No error should happen when create a record without a GORM primary key. But in the database this primary key exists and is the union of 2 or more fields\n But got: %s", err) + } +} + func TestCreateWithNoStdPrimaryKeyAndDefaultValues(t *testing.T) { animal := Animal{Name: "Ferdinand"} if DB.Save(&animal).Error != nil { - t.Errorf("No error should happen when create an record without std primary key") + t.Errorf("No error should happen when create a record without std primary key") } if animal.Counter == 0 { diff --git a/migration_test.go b/migration_test.go index 28176f85..1a7ae6f2 100644 --- a/migration_test.go +++ b/migration_test.go @@ -15,19 +15,11 @@ func runMigration() { DB.Exec(fmt.Sprintf("drop table %v;", table)) } - values := []interface{}{&Product{}, &Email{}, &Address{}, &CreditCard{}, &Company{}, &Role{}, &Language{}, &HNPost{}, &EngadgetPost{}} + values := []interface{}{&Product{}, &Email{}, &Address{}, &CreditCard{}, &Company{}, &Role{}, &Language{}, &HNPost{}, &EngadgetPost{}, &Animal{}, &User{}, &JoinTable{}} for _, value := range values { DB.DropTable(value) } - if err := DB.CreateTable(&Animal{}).Error; err != nil { - panic(fmt.Sprintf("No error should happen when create table, but got %+v", err)) - } - - if err := DB.CreateTable(User{}).Error; err != nil { - panic(fmt.Sprintf("No error should happen when create table, but got %+v", err)) - } - if err := DB.AutoMigrate(values...).Error; err != nil { panic(fmt.Sprintf("No error should happen when create table, but got %+v", err)) } diff --git a/structs_test.go b/structs_test.go index 621dfda4..acd4cc74 100644 --- a/structs_test.go +++ b/structs_test.go @@ -134,6 +134,12 @@ type Animal struct { UpdatedAt time.Time } +type JoinTable struct { + From uint64 + To uint64 + Time time.Time `sql:"default: null"` +} + type Post struct { Id int64 CategoryId sql.NullInt64