Skip to content

fix: update panic if model is not ptr#6037

Merged
jinzhu merged 5 commits intogo-gorm:masterfrom
black-06:fix_callback
Feb 18, 2023
Merged

fix: update panic if model is not ptr#6037
jinzhu merged 5 commits intogo-gorm:masterfrom
black-06:fix_callback

Conversation

@black-06
Copy link
Copy Markdown
Contributor

@black-06 black-06 commented Feb 7, 2023

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

fix #5989.
The same pull request is #5990.
The different is that hook will still be executed when the model is not ptr.

User Case Description

DB.Model(Product5{}).Where("id", p.ID).Update("name", "update_name")

Comment thread callbacks/callmethod.go Outdated
}
case reflect.Struct:
if !db.Statement.ReflectValue.CanAddr() {
db.Statement.ReflectValue = reflect.New(db.Statement.ReflectValue.Type()).Elem()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think maybe better to raise an error?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean that param of Model() method must be ptr?

Comment thread callbacks/callmethod.go Outdated
Comment thread callbacks/callmethod.go
@black-06 black-06 requested a review from a631807682 February 9, 2023 06:43
Comment thread schema/utils.go Outdated
@black-06 black-06 requested a review from jinzhu February 9, 2023 11:06
Comment thread callbacks/callmethod.go
fc(reflect.Indirect(db.Statement.ReflectValue.Index(i)).Addr().Interface(), tx)
if value := reflect.Indirect(db.Statement.ReflectValue.Index(i)); value.CanAddr() {
fc(value.Addr().Interface(), tx)
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we raise an error if the value is not addressable? it should be ok to return an error in those cases would panic before.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

err = DB.Model(Product5{}).Where("id", p.ID).Update("name", "update_name_2").Error

If so, this usage will raise an error, I'm not sure if it's good, it's up to you.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

err = DB.Model(Product5{}).Where("id", p.ID).Update("name", "update_name_2").Error

If so, this usage will raise an error, I'm not sure if it's good, it's up to you.

I thought I sent this message yesterday, but in fact it was pending.... 😨

@black-06 black-06 requested a review from jinzhu February 13, 2023 01:07
Comment thread callbacks/callmethod.go
@jinzhu jinzhu merged commit e66a059 into go-gorm:master Feb 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

if model is not ptr when update record, be panic

3 participants