I want to use the postgres array and I'm using the pq.StringArray as a type for may array which I know and expect that it has implemented the Scanner interface but when I use AutoMigrate(&Post) for example I still get this error:
invalid sql type StringArray (slice) for postgres
import "github.com/lib/pq"
...
type Post struct {
...
Tags pq.StringArray
}
I solved it by using the gorm tag like this:
import "github.com/lib/pq"
...
type Post struct {
...
Tags pq.StringArray `gorm:"type:varchar(64)[]"`
}
I tried it the first time too but there was an accidental space after the gorm: and it wasn't working without any errors like this:
Tags pq.StringArray `gorm: "type:varchar(64)[]"`
anyway it's working now
@mehdy Thanks for your solution. I found a small typo in your snippet
type Post struct {
...
Tags pq.StringArray `gorm:"type:varchar(64)[]"` //Missing " at the end
}
@mehdy no space work for me
I used gorm: "type:text[]"
works fine
anything wrong with that?
It would be great if there was an example in the docs.
This was very helpful.
Most helpful comment
I solved it by using the
gormtag like this:I tried it the first time too but there was an accidental space after the
gorm:and it wasn't working without any errors like this:anyway it's working now