【发布时间】:2020-01-30 03:18:58
【问题描述】:
我在尝试将 JSON 字符串插入 JSON 列时遇到运行时异常。我的字符串看起来像"""{"Events": []}""",表中有一个定义为status JSONB NOT NULL 的列。我可以从命令行将字符串插入表中没有问题。我已经定义了一种插入方法:
import play.api.libs.json._
import anorm._
import anorm.postgresql._
def createStatus(
status: String,
created: LocalDateTime = LocalDateTime.now())(implicit c: SQLConnection): Unit = {
SQL(s"""
|INSERT INTO status_feed
| (status, created)
|VALUES
| ({status}, {created})
|""".stripMargin)
.on(
'status -> Json.parse("{}"), // n.b. would be Json.parse(status) but this provides a concise error message
'created -> created)
.execute()
}
调用它会出现以下错误:
TypeDoesNotMatch(Cannot convert {}: org.postgresql.util.PGobject to String for column ColumnName(status_feed.status,Some(status)))
anorm.AnormException: TypeDoesNotMatch(Cannot convert {}: org.postgresql.util.PGobject to String for column ColumnName(status_feed.status,Some(status)))
我已经对这个问题进行了大量搜索,但我找不到关于这个特定用例的任何内容 - 大部分都是将 json 列提取到案例类中。我尝试了使用 spray-json 的 JsValue、play 的 JsValue 的稍微不同的格式,只需按原样传递字符串并使用 ::JSONB 在查询中进行转换,它们都会给出相同的错误。
更新:这里是创建表的 SQL:
CREATE TABLE status_feed (
id SERIAL PRIMARY KEY,
status JSONB NOT NULL,
created TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT NOW()
)
【问题讨论】:
-
@Ossip 它没有。将值显式包装在 PGobject 中会产生相同的错误消息。
标签: postgresql scala anorm spray-json play-json