Refact callback_create

This commit is contained in:
Jinzhu 2014-06-12 11:54:47 +08:00
parent d2e526bc2d
commit 938618a3e2
1 changed files with 2 additions and 9 deletions

View File

@ -26,15 +26,8 @@ func Create(scope *Scope) {
// set create sql // set create sql
var sqls, columns []string var sqls, columns []string
primaryKeyPredefined := false
for _, field := range scope.Fields() { for _, field := range scope.Fields() {
if field.DBName == scope.PrimaryKey() { if len(field.SqlTag) > 0 && !field.IsIgnored && (field.DBName != scope.PrimaryKey() || !scope.PrimaryKeyZero()) {
primaryKeyValue := fmt.Sprintf("%v", field.Value)
if primaryKeyValue != "" && primaryKeyValue != "0" {
primaryKeyPredefined = true
}
}
if len(field.SqlTag) > 0 && !field.IsIgnored && (field.DBName != scope.PrimaryKey() || primaryKeyPredefined) {
columns = append(columns, scope.Quote(field.DBName)) columns = append(columns, scope.Quote(field.DBName))
sqls = append(sqls, scope.AddToVars(field.Value)) sqls = append(sqls, scope.AddToVars(field.Value))
} }
@ -72,7 +65,7 @@ func Create(scope *Scope) {
} }
} }
if !scope.HasError() && !primaryKeyPredefined { if !scope.HasError() {
scope.SetColumn(scope.PrimaryKey(), id) scope.SetColumn(scope.PrimaryKey(), id)
} }
} }