We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
建议 service.go.tpl 模板文件中 Update 方法忽略 id 字段,否则更新的时候很有可能更改主键
service.go.tpl
Update
id
If you only want to update or ignore some fields when updating, you could use Select, Omit
Select
Omit
before
func Update{{.StructName}}({{.Abbreviation}} *model.{{.StructName}}) (err error) { err = global.GVA_DB.Save({{.Abbreviation}}).Error return err }
after
func Update{{.StructName}}({{.Abbreviation}} *model.{{.StructName}}) (err error) { err = global.GVA_DB.Omit("id").Save({{.Abbreviation}}).Error return err }
The text was updated successfully, but these errors were encountered:
这个隐患可能会在用户修改了更新方法的时候存在,如果直接调用save方法,它会按照主键也就是当前的id进行查找,自动化代码应该不会出现错误覆盖主键的情况。 不过这里可以作为一个,提醒供大家参考,此issue可挂tag保留,感谢!
是的,我在复用 Update 这个方法的时候,发现要传 id ,否则因为没有 id (原记录 id 为零值)会插入新的数据。
Sorry, something went wrong.
No branches or pull requests
建议
service.go.tpl
模板文件中Update
方法忽略id
字段,否则更新的时候很有可能更改主键before
after
The text was updated successfully, but these errors were encountered: