File tree Expand file tree Collapse file tree 1 file changed +13
-10
lines changed
Expand file tree Collapse file tree 1 file changed +13
-10
lines changed Original file line number Diff line number Diff line change @@ -361,27 +361,30 @@ func (sess *session) Save(record db.Record) error {
361361 }
362362
363363 store := record .Store (sess )
364-
365364 if saver , ok := store .(db.StoreSaver ); ok {
366365 return saver .Save (record )
367366 }
368367
369- _ , fields , err := recordPrimaryKeyFieldValues (store , record )
368+ id := db.Cond {}
369+ keys , values , err := recordPrimaryKeyFieldValues (store , record )
370370 if err != nil {
371371 return err
372372 }
373- isCreate := true
374- for i := range fields {
375- if fields [i ] != reflect .Zero (reflect .TypeOf (fields [i ])).Interface () {
376- isCreate = false
377- break
373+ for i := range values {
374+ if values [i ] != reflect .Zero (reflect .TypeOf (values [i ])).Interface () {
375+ id [keys [i ]] = values [i ]
378376 }
379377 }
380378
381- if isCreate {
382- return recordCreate (store , record )
379+ if len (id ) > 0 && len (id ) == len (values ) {
380+ // check if record exists before updating it
381+ exists , _ := store .Find (id ).Count ()
382+ if exists > 0 {
383+ return recordUpdate (store , record )
384+ }
383385 }
384- return recordUpdate (store , record )
386+
387+ return recordCreate (store , record )
385388}
386389
387390func (sess * session ) Delete (record db.Record ) error {
You can’t perform that action at this time.
0 commit comments