diff --git a/callback_create.go b/callback_create.go index a4da39e8..e7fe6f86 100644 --- a/callback_create.go +++ b/callback_create.go @@ -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), )) diff --git a/create_test.go b/create_test.go index 83b3a4ef..92560643 100644 --- a/create_test.go +++ b/create_test.go @@ -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"} diff --git a/dialect.go b/dialect.go index fe8e2f62..b20bfd5b 100644 --- a/dialect.go +++ b/dialect.go @@ -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 diff --git a/dialect_common.go b/dialect_common.go index fbbaef33..b9f0c7da 100644 --- a/dialect_common.go +++ b/dialect_common.go @@ -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, "_")) diff --git a/dialect_mysql.go b/dialect_mysql.go index 1feed1f6..b162bade 100644 --- a/dialect_mysql.go +++ b/dialect_mysql.go @@ -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()" +} diff --git a/dialects/mssql/mssql.go b/dialects/mssql/mssql.go index 1dd5fb69..e0606465 100644 --- a/dialects/mssql/mssql.go +++ b/dialects/mssql/mssql.go @@ -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)