We have a following struct:
Embedding:
type Embedding struct {
Embedded
Name string `gorm:"index"`
<...>
}
Embedded:
type Embedded struct {
ID uuid.UUID `gorm:"primaryKey;type:uuid"`
CreatedAt time.Time `gorm:"index:,sort:desc"`
UpdatedAt time.Time
}
func (e *Embedded) BeforeCreate(tx *gorm.DB) error {
if e.ID == uuid.Nil {
e.ID = uuid.New()
}
return nil
}
This worked fine until we need to define a callback on Embedding:
func (e *Embedding) BeforeCreate(tx *gorm.DB) error {
return tx.Transaction(func(tx *gorm.DB) error {
<...>
})
}
Once this is defined, BeforeCreate on Embedded is not called anymore, so we had to put an explicit call like this:
func (e *Embedding) BeforeCreate(tx *gorm.DB) error {
if err := e.Embedded.BeforeCreate(tx); err != nil {
return err
}
return tx.Transaction(func(tx *gorm.DB) error {
<...>
})
}
I think it would be nice if callbacks on embedded called in both scenarios.
Comment From: github-actions[bot]
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 30 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 ✨
Comment From: inliquid
I think it's pretty clear why this is happening, and would like to change issue type to question.
Comment From: github-actions[bot]
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 30 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 ✨