Description
In my situation, I need convert my object model to another interface. When I Use gorm Create []thatInterface
, got a panic:
type TestModel struct {
Id int `gorm:"primaryKey;autoIncrement:true"`
Name string
}
func (t *TestModel) TestMe() {
t.Id = 1
t.Name = "test"
}
func (t *TestModel) TableName() string {
return "test_model"
}
type TestInterface interface {
TestMe()
}
func TestCreate() error {
db, _ := GetGormDB() // return *gorm.DB
objs1 := []TestModel{
{Name: "test1"},
{Name: "test2"},
}
var objs2 []TestInterface
for _, objs2 := range objs1 {
objs2 = append(objs2, &obj)
}
// create ok
if err := db.Model(&TestModel{}).Create(&objs1).Error; err != nil {
return err
}
// panic
if err := db.Model(&TestModel{}).Create(&objs2).Error; err != nil {
return err
}
return nil
}
panic:
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x949f78]
goroutine 1 [running]:
gorm.io/gorm/callbacks.ConvertToCreateValues(0xc0002fa780)
/usr/local/go/pkg/mod/gorm.io/gorm@v1.25.12/callbacks/create.go:246 +0x358
gorm.io/gorm/callbacks.RegisterDefaultCallbacks.Create.func3(0xc0002eede0)
/usr/local/go/pkg/mod/gorm.io/gorm@v1.25.12/callbacks/create.go:66 +0x136
gorm.io/gorm.(*processor).Execute(0xc000266eb0, 0xc0002f8570?)
/usr/local/go/pkg/mod/gorm.io/gorm@v1.25.12/callbacks.go:130 +0x3c3
gorm.io/gorm.(*DB).Create(0xff4620?, {0xd2cb00, 0xc00000e8e8})
/usr/local/go/pkg/mod/gorm.io/gorm@v1.25.12/finisher_api.go:24 +0xa8
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 ✨