How to Create or Update a record with GORM?

前端 未结 7 1285
悲&欢浪女
悲&欢浪女 2021-01-05 04:21

Gorm has a FirstOrCreate method and a FirstOrInit but how to check afterwards if the record was actually created? I like to create a record if it d

相关标签:
7条回答
  • 2021-01-05 05:09

    Here's example from gorm documentation CRUD section

    user := User{Name: "Jinzhu", Age: 18, Birthday: time.Now()}
    
    db.NewRecord(user) // => returns `true` as primary key is blank
    
    db.Create(&user)
    
    db.NewRecord(user) // => return `false` after `user` created
    
    0 讨论(0)
提交回复
热议问题