Gorm creates records. What's the difference between NewRecord and Create?

gorm creates records. What"s the difference between NewRecord and Create? The code is as follows:

user := User{Name: "Jinzhu", Age: 18, Birthday: time.Now()}
db.NewRecord(user) // => `true`
db.Create(&user)
db.NewRecord(user) // => `user``false`

the code comes from the document: http://gorm.io/zh_CN/docs/cre.

Jan.08,2022

  • NewRecord check whether the primary key exists
  • Create that is, perform the insert operation

for more information, please refer to this stackoverflow

1. The returned values are different
2. From the point of view of the code, the NewRecord operation does not have a hook, and the Create has

Menu