maybe i can Register Callback???

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: keemyb

I've not used Upsert, but say you have the model:

type Foo struct {
  gorm.Model

  FooID string
  OtherField string
}

You can achieve this by first doing an update with a where condition, then checking to see if the update affected anything. If it didn't you know you will have to insert a record:

func upsert(db *gorm.DB, foo *Foo) (wasUpdate bool, err error) {
    updates := db.Model(foo).
        Where("foo_id = ?", foo.FooID).
        Updates(foo)
    if updates.RowsAffected == 0 {
        err = db.Create(foo).Error
        if err != nil {
            return
        }
    } else {
                wasUpdate = true
        }
        return
}

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: dandyhuang

I've not used Upsert, but say you have the model:

``` type Foo struct { gorm.Model

FooID string OtherField string } ```

You can achieve this by first doing an update with a where condition, then checking to see if the update affected anything. If it didn't you know you will have to insert a record:

func upsert(db *gorm.DB, foo *Foo) (wasUpdate bool, err error) { updates := db.Model(foo). Where("foo_id = ?", foo.FooID). Updates(foo) if updates.RowsAffected == 0 { err = db.Create(foo).Error if err != nil { return } } else { wasUpdate = true } return }

Of course this can be achieved, but you must lock the table like for update to ensure atomic。 I test upsert, when db.RowsAffected = 1 is insert,db.RowsAffected = 2 is update。 But I am not sure if it is correct

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: jinzhu

https://gorm.io/docs/create.html#Upsert-On-Conflict

Comment From: jaxsong

use xmax