Gorm: invalid sql type StringArray (slice) for postgres

Created on 3 Nov 2016  路  5Comments  路  Source: go-gorm/gorm

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
}

Most helpful comment

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

All 5 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

corvinusy picture corvinusy  路  3Comments

hypertornado picture hypertornado  路  3Comments

alanyuen picture alanyuen  路  3Comments

koalacxr picture koalacxr  路  3Comments

bramp picture bramp  路  3Comments