gen icon indicating copy to clipboard operation
gen copied to clipboard

Hooks with Updates method

Open rsdutta opened this issue 2 years ago • 3 comments

Hooks with Updates method seems to be broken. We are on the latest version of gen: 0.3.19 and on gorm: 1.24.3. We are trying to run a BeforeUpdate hook for one of our models and are getting an empty struct when attempting to access values in the struct within the hook.

How we call gen methods:

i := c.DB.ORM().InvestmentTransaction
_, err := i.WithContext(ctx).Where(i.UUID.Eq(investmentTransactionUUID)).Updates(updateTransactionParamsMap)

Hook:

func (investmentTransaction *InvestmentTransaction) BeforeUpdate(tx *gorm.DB) (err error) {
	fmt.Println("INVESTMENTTRANSACTION")
	spew.Dump(investmentTransaction)
	fmt.Println("")
	return CreateAuditLog(AuditUpdateOperation, investmentTransaction.getAuditLogParams(tx))
}

result of spew:

(*models.InvestmentTransaction)(0x140002528c0)({
 Model: (gorm.Model) {
  ID: (uint) 0,
  CreatedAt: (time.Time) 0001-01-01 00:00:00 +0000 UTC,
  UpdatedAt: (time.Time) 0001-01-01 00:00:00 +0000 UTC,
  DeletedAt: (gorm.DeletedAt) {
   Time: (time.Time) 0001-01-01 00:00:00 +0000 UTC,
   Valid: (bool) false
  }
 },
 UUID: (uuid.UUID) (len=16 cap=16) 00000000-0000-0000-0000-000000000000,
 InvestmentUUID: (uuid.UUID) (len=16 cap=16) 00000000-0000-0000-0000-000000000000,
 LegalEntityUUID: (uuid.UUID) (len=16 cap=16) 00000000-0000-0000-0000-000000000000,
 EquityValueChange: (int64) 0,
 ShareCountChange: (int64) 0,
 TransactionType: (string) "",
 EffectiveDatetime: (time.Time) 0001-01-01 00:00:00 +0000 UTC,
 Notes: (string) "",
 IsPublished: (bool) false
})

Hooks seem to work fine for BeforeCreate and BeforeDelete. Saw that there was an issue a while ago: https://github.com/go-gorm/gen/issues/353 which was resolved, but this may be a regression since many updates have passed

rsdutta avatar Feb 02 '23 00:02 rsdutta

+1

smile-kindred avatar Feb 13 '23 13:02 smile-kindred

any updates on this? happy to help investigate as well

rsdutta avatar Mar 02 '23 19:03 rsdutta

I also encountered the issue of update hooks not being triggered correctly

But I'm using the Save operation

Whether the Save operation ultimately performs a Create or Update operation.

Ultimately, only Save related hooks and Create related hooks will be triggered.

The hooks of Update were never triggered, but instead incorrectly triggered the hooks of Create.

srackhall avatar Oct 11 '23 15:10 srackhall