Skip to content

Panic with embedded struct #4543

@niondir

Description

@niondir

Playground

Edit: Here is the root cause:
go-gorm/playground#361

Description

Im Getting the panic("reflect: Field index out of range") for

err := db.Set(SettingAutoUpdate, false).Create(entity).Error

Where entity is:

type Model struct {
	Id        int64     `gorm:"type:bigserial;primary_key" json:"id"`
	CreatedAt time.Time `sql:"DEFAULT:NOW()" json:"createdAt"`
	UpdatedAt time.Time `json:"updatedAt"`
}

type Organisation struct {
	Model
	Name       string     `json:"name"`
	Description string
	LogoFileId *int64     `gorm:"default: null" json:"logoFileId"`
	LogoName       string
}

Here is where the panic occurs. tt.fields is ´5.


// create valuer, setter when parse struct
func (field *Field) setupValuerAndSetter() {
	// ValueOf
	switch {
	case len(field.StructField.Index) == 1:
		field.ValueOf = func(value reflect.Value) (interface{}, bool) {
			fieldValue := reflect.Indirect(value).Field(field.StructField.Index[0])
/* [...] */

// Field returns the i'th field of the struct v.
// It panics if v's Kind is not Struct or i is out of range.
func (v Value) Field(i int) Value {
	if v.kind() != Struct {
		panic(&ValueError{"reflect.Value.Field", v.kind()})
	}
	tt := (*structType)(unsafe.Pointer(v.typ))
	if uint(i) >= uint(len(tt.fields)) {
		panic("reflect: Field index out of range")
	}
/* [...] */

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions