Skip to content

Commit

Permalink
Fix insert with default value for mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Feb 12, 2018
1 parent 8005321 commit 3b2c4b3
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion callback_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ func createCallback(scope *Scope) {

if len(columns) == 0 {
scope.Raw(fmt.Sprintf(
"INSERT INTO %v DEFAULT VALUES%v%v",
"INSERT INTO %v %v%v%v",
quotedTableName,
scope.Dialect().DefaultValueStr(),
addExtraSpaceIfExist(extraOption),
addExtraSpaceIfExist(lastInsertIDReturningSuffix),
))
Expand Down
11 changes: 11 additions & 0 deletions create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ func TestCreate(t *testing.T) {
}
}

func TestCreateEmptyStrut(t *testing.T) {
type EmptyStruct struct {
ID uint
}
DB.AutoMigrate(&EmptyStruct{})

if err := DB.Create(&EmptyStruct{}).Error; err != nil {
t.Errorf("No error should happen when creating user, but got %v", err)
}
}

func TestCreateWithExistingTimestamp(t *testing.T) {
user := User{Name: "CreateUserExistingTimestamp"}

Expand Down
2 changes: 2 additions & 0 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type Dialect interface {
SelectFromDummyTable() string
// LastInsertIdReturningSuffix most dbs support LastInsertId, but postgres needs to use `RETURNING`
LastInsertIDReturningSuffix(tableName, columnName string) string
// DefaultValueStr
DefaultValueStr() string

// BuildKeyName returns a valid key name (foreign key, index key) for the given table, field and reference
BuildKeyName(kind, tableName string, fields ...string) string
Expand Down
4 changes: 4 additions & 0 deletions dialect_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ func (commonDialect) LastInsertIDReturningSuffix(tableName, columnName string) s
return ""
}

func (commonDialect) DefaultValueStr() string {
return "DEFAULT VALUES"
}

// BuildKeyName returns a valid key name (foreign key, index key) for the given table, field and reference
func (DefaultForeignKeyNamer) BuildKeyName(kind, tableName string, fields ...string) string {
keyName := fmt.Sprintf("%s_%s_%s", kind, tableName, strings.Join(fields, "_"))
Expand Down
4 changes: 4 additions & 0 deletions dialect_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,7 @@ func (s mysql) BuildKeyName(kind, tableName string, fields ...string) string {

return fmt.Sprintf("%s%x", string(destRunes), bs)
}

func (mysql) DefaultValueStr() string {
return "VALUES()"
}
4 changes: 4 additions & 0 deletions dialects/mssql/mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ func (mssql) LastInsertIDReturningSuffix(tableName, columnName string) string {
return ""
}

func (mssql) DefaultValueStr() string {
return "DEFAULT VALUES"
}

func currentDatabaseAndTable(dialect gorm.Dialect, tableName string) (string, string) {
if strings.Contains(tableName, ".") {
splitStrings := strings.SplitN(tableName, ".", 2)
Expand Down

0 comments on commit 3b2c4b3

Please sign in to comment.