【发布时间】:2015-06-27 00:50:54
【问题描述】:
我正在尝试使文章可标记。
文章表:
type Article struct {
ID int64
Body string
Tags string
}
准备值:
tags := r.FormValue("tags")
tagArray := fmt.Sprintf("%q", strings.Split(tags, ", ")) // How do I make use of this?
t := Article{
Body: "this is a post",
Tags: `{"apple", "orange"}`, // I have to hard code this for this to work.
}
if err := t.Insert(Db); err != nil {
// Error handling
}
数据库查询:
func (t *Article) Insert(db *sqlx.DB) error {
nstmt, err := db.PrepareNamed(`INSERT INTO articles
(body, tags)
VALUES (:body, :tags)
RETURNING *;
`)
if err != nil {
return err
}
defer nstmt.Close()
err = nstmt.QueryRow(t).StructScan(t)
if err, ok := err.(*pq.Error); ok {
return err
}
return err
}
标签字段的 Postgres 设置:
tags character varying(255)[] DEFAULT '{}',
似乎我必须对标签的值进行硬编码才能使其工作。否则我会收到如下错误:
pq: missing dimension value
OR
pq: array value must start with "{" or dimension information
如何使用tagArray?
【问题讨论】:
-
可以显示准确的插入脚本吗??
-
你的意思是数据库查询?我这里有。
-
我的意思是完全限定的插入语句,即
insert into table (col1,col2) values(1,'Some_Data') -
你是这个意思?
INSERT INTO articles (body, tags) VALUES (:body, :tags) RETURNING *;
标签: postgresql go