From ddc1f9156aa0dedd3a865d180bd772a6b85971af Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 29 Apr 2014 17:42:10 +0800 Subject: [PATCH] Fix insert empty record into database --- callback_create.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/callback_create.go b/callback_create.go index d5e56267..e1b739e5 100644 --- a/callback_create.go +++ b/callback_create.go @@ -32,13 +32,20 @@ func Create(scope *Scope) { } } - scope.Raw(fmt.Sprintf( - "INSERT INTO %v (%v) VALUES (%v) %v", - scope.TableName(), - strings.Join(columns, ","), - strings.Join(sqls, ","), - scope.Dialect().ReturningStr(scope.PrimaryKey()), - )) + if len(columns) == 0 { + scope.Raw(fmt.Sprintf("INSERT INTO %v DEFAULT VALUES %v", + scope.TableName(), + scope.Dialect().ReturningStr(scope.PrimaryKey()), + )) + } else { + scope.Raw(fmt.Sprintf( + "INSERT INTO %v (%v) VALUES (%v) %v", + scope.TableName(), + strings.Join(columns, ","), + strings.Join(sqls, ","), + scope.Dialect().ReturningStr(scope.PrimaryKey()), + )) + } // execute create sql var id interface{}