What version of Go are you using (go version)?
go version go1.14 linux/amd64
Which database and its version are you using?
Postgres
psql (PostgreSQL) 9.6.18
My Model which I want to save in DB.
import (
geo "github.com/kellydunn/golang-geo"
)
type Demo struct {
Source geo.Point `json:"source"`
Destination geo.Point `json:"destination"`
}
I'm unable to save source and destination in my postgres DB
Schema of DB is below:
Table "public.demo"
Column | Type | Collation | Nullable | Default
----------------+--------------------------+-----------+----------+---------------------------------
source | point | | not null |
destination | point | | not null |
And the query function which I'm using is :
db.Create(&input)
//where input is of type Demo
Comment From: jinzhu
Hello,
Could you create a reproducible pull request here? https://github.com/go-gorm/playground
Thank you.
Comment From: github-actions[bot]
This issue has been automatically marked as stale as it missing playground pull request link, checkout https://github.com/go-gorm/playground for details, it will be closed in 2 days if no further activity occurs.
Comment From: bravecheng
@tyagip966 Did you solve it? I customized the Point, but I still get an error.
device.go
type Device struct {
Location Point `json:"location" gorm:"type:point"`
}
point.go
type Point struct {
X float64 `json:"lat"`
Y float64 `json:"lng"`
}
func (p *Point) Value() (driver.Value, error) {
buf := new(bytes.Buffer)
fmt.Fprintf(buf, "(%f %f)", p.X, p.Y)
return buf.Bytes(), nil
}
func (p *Point) String() string {
return fmt.Sprintf("(%v %v)", p.X, p.Y)
}
func (p *Point) Scan(val interface{}) (err error) {
if bb, ok := val.([]uint8); ok {
tmp := bb[1 : len(bb)-1]
coors := strings.Split(string(tmp[:]), ",")
if p.X, err = strconv.ParseFloat(coors[0], 64); err != nil {
return err
}
if p.Y, err = strconv.ParseFloat(coors[1], 64); err != nil {
return err
}
}
return nil
}
Comment From: yuseferi
@bravecheng have you got an answer for this?
Comment From: 1saifj
Any answers to this?
Comment From: jinzhu
func (p *Point) Value() should be func (p Point) Value()
Comment From: random-char
Added commas to have (x, y) format and it worked for me. Using go 1.21.4 and postgres 13.13
```go func (p *Point) Value() (driver.Value, error) { buf := new(bytes.Buffer) fmt.Fprintf(buf, "(%f, %f)", p.X, p.Y) return buf.Bytes(), nil }
func (p *Point) String() string { return fmt.Sprintf("(%v, %v)", p.X, p.Y) } ```