Skip to content

AfterDelete Hook only works with a Delete(&struct) with a struct with PK value populated #792

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

keif888
Copy link

@keif888 keif888 commented Mar 7, 2025

Explain your user case and expected results

When executing a Delete() via GORM, the AfterDelete hook will fire. What the AfterDelete hook receives is the struct that was in the Delete().
If a Where("id=?", 10).Delete(&struct{}) clause is used or an ID is populated via Delete(&struct{}, 10) then the AfterDelete receives an empty struct.

The documentation on gorm.io should reflect what AfterDelete receives so that developers can use AfterDelete appropriately.

  • Use Case 1 - Struct populated with ID
    • AfterDelete is called ONCE and receives the struct with ID (and other fields if provided), UpdatedAt, and DeletedAt populated
    • Test structdelete shows this
  • Use Case 2 - Struct without ID, and list of ID(s) provided in delete conditions
    • AfterDelete is called ONCE and receives the struct without ID (but other fields if provided), UpdatedAt, and DeletedAt populated
    • Test structdeletebyid shows this
  • Use Case 3 - Empty Struct, Delete using Where clause
    • AfterDelete is called ONCE and receives a struct with UpdatedAt, and DeletedAt populated
    • Tests singledeletebyid, singledelete and bulkdelete shows these

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

Successfully merging this pull request may close these issues.

1 participant