Your Question
Hello.
I have a system where several microservices uses Gorm. I would like to update Gorm to set the field UpdatedAt
to the same timestamp DeletedAt
get set to during a soft-delete.
Because there are many microservices with many models, I would like to do this without having to modify the model struct themselves. Ideally via a hook or something similar.
I have tried using a global hook to modify the clause.Set
of the statement to include UpdatedAt, but it seems that soft-delete.go
will overwrite it.
How can I go about doing this?
The document you expected this should be explained
https://gorm.io/docs/hooks.html
Expected answer
It would be great if I could define a global hook and modify the Set statement before the update/delete query is called.
db.Callback().Delete().Before("gorm:delete").Register(hookName, updateUpdatedAtWithSoftDelete)
func updateUpdatedAtWithSoftDelete(db *gorm.DB) {
//Overwrite the Set query here
}
Query I want
UPDATE `database_name`.`table_name` SET `deleted_at`=?, `updated_at`=? WHERE `database_name`.`id` = ? AND `database_name`.`deleted_at` IS NULL`
Thank you for your assistance.
Comment From: a631807682
refer to https://gorm.io/docs/sql_builder.html#Clauses https://github.com/go-gorm/gorm/blob/master/soft_delete.go
Comment From: liranddd
@Stefancomvex https://medium.com/tailor-tech/gorm-soft-delete-how-to-update-the-updated-at-field-automatically-7da7817f796a