dongyihao1099 2018-02-14 12:49
浏览 201

Create和NewRecord有什么区别?

According to the document and gorm repo's issue,
db.Create is insert record.
db.NewRecord is only check if current value has primary key or not. So do not insert record.

I wrote below this code.
But I could insert record with db.Create and db.NewRecord.

What's different between this two methods?

for _, v := range oneday.Onedays {
    if err := tx.Create(&v).Error; err != nil {
        tx.Rollback()
        return model.ResOnedays{}, err
    }

    // Photos
    for i, pv := range v.Photos {
        res := tx.NewRecord(&pv)
    }

    // Tags
    for _, tv := range v.Tags {
        tx.NewRecord(&tv)
    }
}
  • 写回答

1条回答 默认 最新

  • douting1871 2018-02-14 13:01
    关注

    As you pointed out, Create creates a new row by issuing an INSERT.

    NewRecord doesn't affect the database at all, it just returns true if the current value's primary key is unset, meaning it's a new record. This means calling NewRecord in void context, as you have done, is meaningless, since you're ignoring the return value.

    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改