Describe the feature

NOTE When update with struct, GORM will only update non-zero fields, you might want to use map to update attributes or use Select to specify fields to update

注意 当通过 struct 更新时,GORM 只会更新非零字段。 如果您想确保指定字段被更新,你应该使用 Select 更新选定字段,或使用 map 来完成更新操作

There should be an alternative method compatible with the selected update zero value of struct. The current use of SELECT to qualify is very limited.

应该有一个额外的方法来选定一些struct里的字段,使他们即使是零值也可以被更新. 当前使用select来限定是有很大局限性的

Motivation

有一些场景是事先并不知道可能会更新哪些字段,所以使用struct来对应,但是希望有需要当其中某些字段是0值的时候也能够更新这些字段, 除此之外的字段仍然是仅更新非0值.

There are some scenarios where you don't know in advance which fields are likely to be updated, so use struct accordingly, but you need to be able to update those fields even if some of them have a zero value, and otherwise only update non-zero values

Related Issues

nothing.


I could submit a PR to do this, but I can't think of a good method name. 😂😂😂

Comment From: github-actions[bot]

This issue has been automatically marked as stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days

Comment From: zlasd

This feature will be handy in some scenes. Please consider support it.

I have a big struct and there is an API to update some fields which I cannot know in advance. I only know which columns should be updated even if they have zero value. In this case, both Select and Omit are infeasible.

Comment From: a631807682

It seems that this situation is rarely encountered, but it is very cumbersome every time. I not sure it needs to be added to gorm, so i create a plugin to support it. https://github.com/a631807682/zerofield