Save All Fields
Save
will save all fields when performing the Updating SQL
db.First(&user) |
Update single column
When updating a single column with Update
, it needs to have any conditions or it will raise error ErrMissingWhereClause
, checkout Block Global Updates for details
When using the Model
method and its value has a primary value, the primary key will be used to build the condition, for example:
// Update with conditions |
Updates multiple columns
Updates
supports update with struct
or map[string]interface{}
, when updating with struct
it will only update non-zero fields by default
// Update attributes with `struct`, will only update non-zero fields |
NOTE When update with struct, GORM will only update non-zero fields, you might want to use
map
to update attributes or useSelect
to specify fields to update
Update Selected Fields
If you want to update selected fields or ignore some fields when updating, you can use Select
, Omit
// Select with Map |
Update Hooks
GORM allows hooks BeforeSave
, BeforeUpdate
, AfterSave
, AfterUpdate
, those methods will be called when updating a record, refer Hooks for details
func (u *User) BeforeUpdate(tx *gorm.DB) (err error) { |
Batch Updates
If we haven’t specified a record having primary key value with Model
, GORM will perform a batch updates
// Update with struct |
Block Global Updates
If you perform a batch update without any conditions, GORM WON’T run it and will return ErrMissingWhereClause
error by default
You have to use some conditions or use raw SQL or enable the AllowGlobalUpdate
mode, for example:
db.Model(&User{}).Update("name", "jinzhu").Error // gorm.ErrMissingWhereClause |
Updated Records Count
Get the number of rows affected by a update
// Get updated records count with `RowsAffected` |
Advanced
Update with SQL Expression
GORM allows updates column with SQL expression, e.g:
// product's ID is `3` |
And GORM also allows update with SQL Expression/Context Valuer with Customized Data Types, e.g:
// Create from customized data type |
Update from SubQuery
Update a table by using SubQuery
DB.Model(&user).Update("company_name", DB.Model(&Company{}).Select("name").Where("companies.id = users.company_id")) |
Without Hooks/Time Tracking
If you want to skip Hooks
methods and don’t track the update time when updating, you can use UpdateColumn
, UpdateColumns
, it works like Update
, Updates
// Update single column |
Check Field has changed?
GORM provides Changed
method could be used in Before Update Hooks, it will return the field changed or not
The Changed
method only works with methods Update
, Updates
, and it only checks if the updating value from Update
/ Updates
equals the model value, will return true if it is changed and not omitted
func (u *User) BeforeUpdate(tx *gorm.DB) (err error) { |
Change Updating Values
To change updating values in Before Hooks, you should use scope.SetColumn
unless it is a full updates with Save
, for example:
func (user *User) BeforeSave(scope *gorm.Scope) (err error) { |