I have the following simple struct
type Profile struct {
gorm.Model
Email string `json:"email" sql:"not null;unique"`
LastLogin time.Time `json:"lastlogin"`
}
I'm trying to make a insert if it doesn't exist via
db.Con.Debug().Where(db.Profile{Email: "user@domain.com"}).Assign(db.Profile{LastLogin: time.Now()}).FirstOrCreate(&profile)
I get the following in my logs
(/Users/mzupan/go/src/gitlab.com/org/app/pkg/auth/login.go:182)
[2018-09-24 13:35:58] [4.56ms] SELECT * FROM "profiles" WHERE "profiles"."deleted_at" IS NULL AND (("profiles"."email" = 'user@domain.com')) ORDER BY "profiles"."id" ASC LIMIT 1
[0 rows affected or returned ]
(/Users/mzupan/go/src/gitlab.com/org/app/pkg/auth/login.go:182)
[2018-09-24 13:35:58] [1.77ms] UPDATE "profiles" SET "last_login" = '2018-09-24 13:35:58', "updated_at" = '2018-09-24 13:35:58' WHERE "profiles"."deleted_at" IS NULL AND (("profiles"."email" = 'user@domain.com'))
[0 rows affected or returned ]
So it's trying to do the select/update even though 0 rows are found in the select. Seems to me I'm doing the right thing.