PostgreSQL JSON字段无法插入数据库 #662

Closed
opened 2017-07-26 16:11:27 +00:00 by AbooJan · 1 comment
AbooJan commented 2017-07-26 16:11:27 +00:00 (Migrated from github.com)

数据库: PostgreSQL
xorm: 最新版

比如我有一个user表在数据库中定义为:

id bigint 
name varchar(20)
extras json

对于字段 extras 可以为空


使用xorm命令行工具生成数据bean为:

type User struct {
   Id               int64     `xorm:"pk autoincr unique BIGINT"`
   Name           string    `xorm:"VARCHAR(20)"`
   Extras          string    `xorm:"JSON"`
}

当我用xorm 插入一条记录:

user := models.User{}
user.Name = "Aboo"

无法完成,总是报错:

pq: invalid input syntax for type json

如果我把数据库表中 extras 字段的类型改成 text , 才能插入成功。

请问这是因为 xorm 不知道 pgJSON类型 吗?
只能用 text 类型替代么?

万分感谢,希望可以解答一下?

数据库: PostgreSQL xorm: 最新版 比如我有一个user表在数据库中定义为: ``` id bigint name varchar(20) extras json ``` 对于字段 `extras` 可以为空 --- 使用xorm命令行工具生成数据bean为: ``` type User struct { Id int64 `xorm:"pk autoincr unique BIGINT"` Name string `xorm:"VARCHAR(20)"` Extras string `xorm:"JSON"` } ``` 当我用xorm 插入一条记录: ``` user := models.User{} user.Name = "Aboo" ``` 无法完成,总是报错: ``` pq: invalid input syntax for type json ``` --- 如果我把数据库表中 `extras` 字段的类型改成 `text` , 才能插入成功。 请问这是因为 `xorm` 不知道 `pg` 的 `JSON类型` 吗? 只能用 `text` 类型替代么? 万分感谢,希望可以解答一下?
type User struct {
   Id               int64     `xorm:"pk autoincr unique BIGINT"`
   Name           string    `xorm:"VARCHAR(20)"`
   Extras          interface{}    `xorm:"JSON"`
}

结构体中的Extras改成interface{}或者其它的数据结构,JSON不能应用于string,这个可能是生成工具的Bug

```Go type User struct { Id int64 `xorm:"pk autoincr unique BIGINT"` Name string `xorm:"VARCHAR(20)"` Extras interface{} `xorm:"JSON"` } ``` 结构体中的Extras改成interface{}或者其它的数据结构,JSON不能应用于string,这个可能是生成工具的Bug
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#662
No description provided.