所以我使用go-gorp来查询postgres,我似乎无法查询我的表中的复合类型是错误的。我想要的只是适当嵌套的JSON响应。我的postgres架构是:
CREATE TYPE PhoneType AS ENUM ('MOBILE', 'HOME', 'WORK');
CREATE TYPE PhoneNumber AS (
"Number" VARCHAR,
"Type" PhoneType
);
CREATE TABLE Person (
"Id" SERIAL PRIMARY KEY NOT NULL,
"Name" VARCHAR NOT NULL,
"Email" VARCHAR,
"Number" PhoneNumber[]
);
相应地,在golang,我有
const (
MOBILE PhoneType = iota
HOME
WORK
)
type PhoneNumber struct {
Number string
Type PhoneType
}
type Person struct {
Id int
Name string
Email string
PhoneNumber // not sure how to get array of phone numbers here
}
要查询,我使用go-gorp如下:
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.PostgresDialect{}}
var data []Person
_, err := dbmap.Select(&data, "SELECT * FROM Person")
我得到的结果是格式
{
"Id":
"Name":
"Email"
"Number": "{\"(..., ...)\"}"
"Type": 0
}
我期待的是:
{
"Id":
"Name":
"Email":
"PhoneNumber": [{
"Number":
"Type":
}]
}
如何将此嵌套复合类型更改为go中声明的类型友好?
编辑:看起来像postres数组和复合类型成为go中的字符串。如何重新设计模式以获得类似的结果?
答案 0 :(得分:0)
您的类型声明错误,请尝试以下示例:
type Phone struct {
Number string
Type PhoneType
}
type Person struct {
Id int
Name string
Email string
PhoneNumber []Phone
}