diff --git a/schema/schema.go b/schema/schema.go index e5894443..72bc6544 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -73,7 +73,7 @@ type Tabler interface { // get data type from dialector func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error) { modelType := reflect.ValueOf(dest).Type() - for modelType.Kind() == reflect.Slice || modelType.Kind() == reflect.Ptr { + for modelType.Kind() == reflect.Slice || modelType.Kind() == reflect.Array || modelType.Kind() == reflect.Ptr { modelType = modelType.Elem() } diff --git a/tests/create_test.go b/tests/create_test.go index 4bf623b3..75059f18 100644 --- a/tests/create_test.go +++ b/tests/create_test.go @@ -189,6 +189,48 @@ func TestPolymorphicHasOne(t *testing.T) { CheckPet(t, *pet, *pet) } }) + + t.Run("Array", func(t *testing.T) { + var pets = [...]Pet{{ + Name: "PolymorphicHasOne-Array-1", + Toy: Toy{Name: "Toy-PolymorphicHasOne-Array-1"}, + }, { + Name: "PolymorphicHasOne-Array-2", + Toy: Toy{Name: "Toy-PolymorphicHasOne-Array-2"}, + }, { + Name: "PolymorphicHasOne-Array-3", + Toy: Toy{Name: "Toy-PolymorphicHasOne-Array-3"}, + }} + + if err := DB.Create(&pets).Error; err != nil { + t.Fatalf("errors happened when create: %v", err) + } + + for _, pet := range pets { + CheckPet(t, pet, pet) + } + }) + + t.Run("ArrayPtr", func(t *testing.T) { + var pets = [...]*Pet{{ + Name: "PolymorphicHasOne-Array-1", + Toy: Toy{Name: "Toy-PolymorphicHasOne-Array-1"}, + }, { + Name: "PolymorphicHasOne-Array-2", + Toy: Toy{Name: "Toy-PolymorphicHasOne-Array-2"}, + }, { + Name: "PolymorphicHasOne-Array-3", + Toy: Toy{Name: "Toy-PolymorphicHasOne-Array-3"}, + }} + + if err := DB.Create(&pets).Error; err != nil { + t.Fatalf("errors happened when create: %v", err) + } + + for _, pet := range pets { + CheckPet(t, *pet, *pet) + } + }) } func TestCreateEmptyStruct(t *testing.T) {