Hello, what about postgresql arrays? As I understand if the type of field is slice - gorm interprets it like associations. And if it's []string for example there is go panic: "reflect.Value.FieldByName on string Value"
I know MySQL don't have an array type (and this is very strange I think), but Postgresql uses it heavily.
I think I found a solution.
var Test struct {
Scopes string
ScopesArray []string `json:"-" sql:"-"`
}
func (m *Test) ToPGArray() {
... // logic to convert slice to string like "{hello, world}". Simple strings.Split will do.
}
func (m *Test) FromPGArray() {
... // logic to convert string to array. Simple strings.Split will do.
}
func (m *Test) BeforeSave() (err error) {
m.ToPGArray()
...
}
func (m *Test) AfterFind() (err error) {
m.FromPGArray()
}
Then all you have to do is:
dbConn.Save(Test{ScopesArray: []string{"hello", "world"}})
Method above doesn't work because any slice interprets like association I guess. Even tag: sql:"-" doesn't work, so no internal custom array fields :)
HI @reterius
You could use the new plugin system to do this. https://github.com/jinzhu/gorm/blob/master/doc/development.md
With it, you could rewrite some callbacks and do everything. I would like to write an example but kind of too busy to do this now ;(
If you want to give it a try, let me know if you need any help, thank you.
The panic thing should be fixed. I think maybe better, you should write a Scanner to handle those things.
"github.com/lib/pq" can add array datatype feature to gorm
something like that:
import (
...
"github.com/lib/pq"
...
)
...
type Block struct {
gorm.Model
.....
Tx pq.StringArray `gorm:"type:varchar(100)[]"`
}
Is there a similar thing for UUID array?
Most helpful comment
"github.com/lib/pq"can add array datatype feature to gormsomething like that: