Skip to content

Commit

Permalink
Avoid Errors in postgres when creating a row without a GORM defined p…
Browse files Browse the repository at this point in the history
…rimary key (but defined db-side)
  • Loading branch information
galeone committed Dec 8, 2014
1 parent cbcb88d commit 0fa1335
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
11 changes: 9 additions & 2 deletions callback_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,25 @@ 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(
"INSERT INTO %v (%v) VALUES (%v) %v",
scope.QuotedTableName(),
strings.Join(columns, ","),
strings.Join(sqls, ","),
scope.Dialect().ReturningStr(scope.PrimaryKey()),
scope.Dialect().ReturningStr(returningField),
))
}

Expand Down
10 changes: 9 additions & 1 deletion create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 1 addition & 9 deletions migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
6 changes: 6 additions & 0 deletions structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0fa1335

Please sign in to comment.