mirror of https://github.com/go-gorm/gorm.git
Fix insert with default value for mysql
This commit is contained in:
parent
8005321a1c
commit
3b2c4b3608
|
@ -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),
|
||||||
))
|
))
|
||||||
|
|
|
@ -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"}
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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, "_"))
|
||||||
|
|
|
@ -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()"
|
||||||
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
|
Loading…
Reference in New Issue