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

How to update UpdatedAt along with DeletedAt on soft-delete. #7385

Open
Stefancomvex opened this issue Mar 8, 2025 · 1 comment
Open

How to update UpdatedAt along with DeletedAt on soft-delete. #7385

Stefancomvex opened this issue Mar 8, 2025 · 1 comment
Assignees
Labels
type:question general questions

Comments

@Stefancomvex
Copy link

Stefancomvex commented Mar 8, 2025

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.

@Stefancomvex Stefancomvex added the type:question general questions label Mar 8, 2025
@a631807682
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question general questions
Projects
None yet
Development

No branches or pull requests

3 participants