Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to do an unscoped preload in Golang GORM?

Tags:

go-gorm

Adding Unscoped() to the call chain like this:

db.Unscoped().Preload("Orders").Find(&users)

affects the Find(), but does not affect the Preload().

The query generated for the Preload() still contains:

"orders"."deleted_at" IS NULL

How can I unscope the generated preload query? I want soft-deleted rows to be fetched by Preload().

like image 718
pughar Avatar asked Sep 07 '25 11:09

pughar


1 Answers

Callback can be like this:

.Preload("Orders", func(db *gorm.DB) *gorm.DB {
   return db.Unscoped() 
}
like image 152
Liam Zee Avatar answered Sep 10 '25 06:09

Liam Zee