pughar
pughar

Reputation: 148

Is it possible to do an unscoped preload in Golang 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().

Upvotes: 4

Views: 3926

Answers (1)

Liam Zee
Liam Zee

Reputation: 206

Callback can be like this:

.Preload("Orders", func(db *gorm.DB) *gorm.DB {
   return db.Unscoped() 
}

Upvotes: 15

Related Questions