Search code examples
gohookgo-gormquerying

The Gorm library in Go has an AfterFind-hook. Is it possible to choose if this hook is called automaticlly or not on a query method?


The Gorm library in Go has defined some hook methods. I know from the documentation that you can choose if you want to call on the different update-hooks by choosing the right update method (UpdateColumn vs Update for example).

Is there something similar for the AfterFind-hook? I.e, some query method will call the hook automatically and some will not. That would be super.


Solution

  • I think the answer to give an exception for the AfterFind hook on a query is to use gorms session method. Something like this: DB.Session(&gorm.Session{SkipHooks: true}).Find(&user)