The method of executing SQL in golang gorm Update Log
This article mainly introduces the golang gorm update log implementation of SQL related knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe that everyone after reading this golang gorm update log implementation of SQL method article will have a harvest, let's take a look at it.
1. Update log 1.1. V1.01.1.1. Destructive change
Gorm.Open return type is * gorm.DB instead of gorm.DB
Updating only updates the changed fields
Most applications will not be affected, and only when you change the update value in the callback, such as BeforeSave,BeforeUpdate, you should use scope.SetColumn, for example:
Func (user * User) BeforeUpdate (scope * gorm.Scope) {if pw, err: = bcrypt.GenerateFromPassword (user.Password, 0); err = = nil {scope.SetColumn ("EncryptedPassword", pw) / / user.EncryptedPassword = pw / / does not work, EncryptedPassword field will not be included in the update}}
The default query scope of soft deletion only checks deleted_at IS NULL
Previously, it will check that the deleted_at is less than 0001-01-02 and exclude blank time, such as:
SELECT * FROM users WHERE deleted_at IS NULL OR deleted_at