bug fixed

This commit is contained in:
Lunny Xiao 2013-06-16 15:10:35 +08:00
parent 26ce60b972
commit 9935922176
2 changed files with 15 additions and 5 deletions

View File

@ -132,7 +132,7 @@ func (engine *Engine) AutoMapType(t reflect.Type) *Table {
table, ok := engine.Tables[t] table, ok := engine.Tables[t]
if !ok { if !ok {
table = engine.MapType(t) table = engine.MapType(t)
//engine.Tables[t] = table engine.Tables[t] = table
} }
return table return table
} }

View File

@ -673,9 +673,13 @@ func (session *Session) InsertOne(bean interface{}) (int64, error) {
if col.IsAutoIncrement && fieldValue.Int() == 0 { if col.IsAutoIncrement && fieldValue.Int() == 0 {
continue continue
} }
if table, ok := session.Engine.Tables[fieldValue.Type()]; ok { if fieldTable, ok := session.Engine.Tables[fieldValue.Type()]; ok {
pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumn().FieldName) if fieldTable.PrimaryKey != "" {
pkField := reflect.Indirect(fieldValue).FieldByName(fieldTable.PKColumn().FieldName)
args = append(args, pkField.Interface()) args = append(args, pkField.Interface())
} else {
continue
}
} else { } else {
args = append(args, val) args = append(args, val)
} }
@ -696,10 +700,16 @@ func (session *Session) InsertOne(bean interface{}) (int64, error) {
} }
id, err := res.LastInsertId() id, err := res.LastInsertId()
if err != nil { if err != nil {
return -1, err return -1, err
} }
if id > 0 && table.PrimaryKey != "" {
pkValue := reflect.Indirect(reflect.ValueOf(bean)).FieldByName(table.PKColumn().FieldName)
if pkValue.CanSet() {
var v interface{} = id
pkValue.Set(reflect.ValueOf(v))
}
}
return id, nil return id, nil
} }