Skip to content

Combine Preload with Updates doesn't seem to return preloaded result unless if chained with First #7660

@lainas262

Description

@lainas262

Your Question

  1. I am trying to update a record and I want to return back the updated entry along with preloaded data. If I do this:

result := r.db.Model(&principal).Clauses(clause.Returning{}).Preload("Creator", func(db *gorm.DB) *gorm.DB { return db.Unscoped().Select("id", "first_name", "last_name", "email", "deleted_at") }).Preload("Updater", func(db *gorm.DB) *gorm.DB { return db.Unscoped().Select("id", "first_name", "last_name", "email", "deleted_at") }).Updates(&principal).First(&principal)

I seem to get the desired effect - however I am not sure if this is the correct approach as it's not documented. This could also be achieved using a transaction, or alternatively with separate update, select operations. Is there a way to achieve this in a simple atomic way?

  1. Additionally correct me if I am wrong, there's is no way using the generics api yet to preload soft deleted entries?

Thanks

The document you expected this should be explained

For Question 1:
Shows how to return data during an update but it seems like this doesn't take into consideration preloading. Additionally chaining it with First seems to yield the expected result. Although I am not sure if that's expected.
https://gorm.io/docs/update.html#Returning-Data-From-Modified-Rows

For question 2:
The generics way doesn't describe preloading: https://gorm.io/docs/the_generics_way.html
Preloading (Eager Loading) doesn't seem to cover retrieving soft deleted values: https://gorm.io/docs/preload.html

Expected answer

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions