Fix insert with default value for mysql

This commit is contained in:
Jinzhu 2018-02-12 17:39:34 +08:00
parent 8005321a1c
commit 3b2c4b3608
6 changed files with 27 additions and 1 deletions

View File

@ -97,8 +97,9 @@ func createCallback(scope *Scope) {
if len(columns) == 0 { if len(columns) == 0 {
scope.Raw(fmt.Sprintf( scope.Raw(fmt.Sprintf(
"INSERT INTO %v DEFAULT VALUES%v%v", "INSERT INTO %v %v%v%v",
quotedTableName, quotedTableName,
scope.Dialect().DefaultValueStr(),
addExtraSpaceIfExist(extraOption), addExtraSpaceIfExist(extraOption),
addExtraSpaceIfExist(lastInsertIDReturningSuffix), addExtraSpaceIfExist(lastInsertIDReturningSuffix),
)) ))

View File

@ -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) { func TestCreateWithExistingTimestamp(t *testing.T) {
user := User{Name: "CreateUserExistingTimestamp"} user := User{Name: "CreateUserExistingTimestamp"}

View File

@ -42,6 +42,8 @@ type Dialect interface {
SelectFromDummyTable() string SelectFromDummyTable() string
// LastInsertIdReturningSuffix most dbs support LastInsertId, but postgres needs to use `RETURNING` // LastInsertIdReturningSuffix most dbs support LastInsertId, but postgres needs to use `RETURNING`
LastInsertIDReturningSuffix(tableName, columnName string) string 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 returns a valid key name (foreign key, index key) for the given table, field and reference
BuildKeyName(kind, tableName string, fields ...string) string BuildKeyName(kind, tableName string, fields ...string) string

View File

@ -159,6 +159,10 @@ func (commonDialect) LastInsertIDReturningSuffix(tableName, columnName string) s
return "" 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 // 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 { func (DefaultForeignKeyNamer) BuildKeyName(kind, tableName string, fields ...string) string {
keyName := fmt.Sprintf("%s_%s_%s", kind, tableName, strings.Join(fields, "_")) keyName := fmt.Sprintf("%s_%s_%s", kind, tableName, strings.Join(fields, "_"))

View File

@ -185,3 +185,7 @@ func (s mysql) BuildKeyName(kind, tableName string, fields ...string) string {
return fmt.Sprintf("%s%x", string(destRunes), bs) return fmt.Sprintf("%s%x", string(destRunes), bs)
} }
func (mysql) DefaultValueStr() string {
return "VALUES()"
}

View File

@ -183,6 +183,10 @@ func (mssql) LastInsertIDReturningSuffix(tableName, columnName string) string {
return "" return ""
} }
func (mssql) DefaultValueStr() string {
return "DEFAULT VALUES"
}
func currentDatabaseAndTable(dialect gorm.Dialect, tableName string) (string, string) { func currentDatabaseAndTable(dialect gorm.Dialect, tableName string) (string, string) {
if strings.Contains(tableName, ".") { if strings.Contains(tableName, ".") {
splitStrings := strings.SplitN(tableName, ".", 2) splitStrings := strings.SplitN(tableName, ".", 2)