Skip to content
New issue

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 字段 #140

Open
yann0917 opened this issue Jul 10, 2020 · 2 comments
Open

【建议】 service.go.tpl Update 方法忽略 id 字段 #140

yann0917 opened this issue Jul 10, 2020 · 2 comments
Labels
good first issue Good for newcomers

Comments

@yann0917
Copy link

建议 service.go.tpl 模板文件中 Update 方法忽略 id 字段,否则更新的时候很有可能更改主键

If you only want to update or ignore some fields when updating, you could use 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
}
@yann0917
Copy link
Author

这个隐患可能会在用户修改了更新方法的时候存在,如果直接调用save方法,它会按照主键也就是当前的id进行查找,自动化代码应该不会出现错误覆盖主键的情况。 不过这里可以作为一个,提醒供大家参考,此issue可挂tag保留,感谢!

是的,我在复用 Update 这个方法的时候,发现要传 id ,否则因为没有 id (原记录 id 为零值)会插入新的数据。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants
@yann0917 and others