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

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eba5e5-4858a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eba5e5-4858a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?