You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
i'm experiencing a problem with setting values using the BeforeSave and BeforeUpdate hooks while updating the data using a map (to do a partial update).
In brief what i'm doing right now is:
Unmarshal the values into a map to use it for the partial update
Update them via the db.Model(&model).Updates(&values) methods
Apply some changes to the data in the BeforeSave and BeforeUpdate hooks
Applying the data to the model of the hooks here doesn't work because the values to be used are in the map.
Here's where SetColumn comes to the rescue.
It fits perfectly my use case ... but it doesn't work as expected.
Let's take this exampleModel in consideration:
exampleModel.go
...
type ExampleModel struct {
...
Name string `gorm:"type:nvarchar(150);"`
...
}
func (ExampleModel) BeforeUpdate(db *gorm.DB) error {
db.Statement.SetColumn("Name", "Fulmineo")
return db.Error
// Doesn't work returns "invalid field"
}
...
This doesn't work because the db param isn't the "parent db" that contains the Statement with the Model, Schema and Dest properties with the correct values.
I understand the need to create a new Session which is essential to avoid unexpected behaviours on the hook.
Unfortunately this invalidates the use of SetColumn in these cases as the Statement on which it gets called is different.
...
type ExampleModel struct {
...
Name string `gorm:"type:nvarchar(150);"`
...
}
func (ExampleModel) BeforeUpdate(db *gorm.DB) error {
tx := db.Statement.Context.Value("parent").(*gorm.DB)
tx.Statement.SetColumn("Name", "Fulmineo")
// Works flawlessly as expected, updating the value on the map and applying it to the database
return nil
}
...
Is there a better way to do this?
The problem
The gorm's callMethod creates a new Session and this unfortunately compromises the use of SetColumn in BeforeSave and BeforeUpdate hooks like in the documentation.
Possible solution
Like my temporary solution using the gorm's Context, it could be useful to have a "parent" property inside the Statement to refer to it for updating the values when working with maps.
I'm open to discuss other possible solutions.
Thank you for your collaboration.
The text was updated successfully, but these errors were encountered:
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Hi jinzhu,
i'm experiencing a problem with setting values using the BeforeSave and BeforeUpdate hooks while updating the data using a map (to do a partial update).
In brief what i'm doing right now is:
Applying the data to the model of the hooks here doesn't work because the values to be used are in the map.
Here's where SetColumn comes to the rescue.
It fits perfectly my use case ... but it doesn't work as expected.
Let's take this exampleModel in consideration:
exampleModel.go
This doesn't work because the db param isn't the "parent db" that contains the Statement with the Model, Schema and Dest properties with the correct values.
The reference of the "parent db" gets lost here:
gorm/callbacks/callmethod.go
I understand the need to create a new Session which is essential to avoid unexpected behaviours on the hook.
Unfortunately this invalidates the use of SetColumn in these cases as the Statement on which it gets called is different.
I've temporarily solved the problem like this:
gorm/callbacks/callmethod.go
And in the exampleModel.go
exampleModel.go
Is there a better way to do this?
The problem
The gorm's callMethod creates a new Session and this unfortunately compromises the use of SetColumn in BeforeSave and BeforeUpdate hooks like in the documentation.
Possible solution
Like my temporary solution using the gorm's Context, it could be useful to have a "parent" property inside the Statement to refer to it for updating the values when working with maps.
I'm open to discuss other possible solutions.
Thank you for your collaboration.
The text was updated successfully, but these errors were encountered: