-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Description
Your Question
- 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?
- 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