我正在运行游戏2.3并且无法更新...
我试图解析jsonb列时遇到错误play - Cannot invoke the action, eventually got an error: java.lang.RuntimeException: UnexpectedNullableFound(ColumnName(.sports,Some(sports)))
我的代码是:
case class Sport(
id: UUID,
name: Option[String]
)
implicit val SportWrites: Writes[Sport] = Json.writes[Sport]
implicit val SportReads: Reads[Sport] = (
(JsPath \ "f1").read[UUID] and
(JsPath \ "f2").readNullable[String]
)
case class Member(
id:UUID,
name:String,
sports: List[Sport]
)
def memberParser: RowParser[Member] = {
get[UUID]("member_id") ~
get[String]("name") ~
get[JsValue]("sports") map {
case(id ~ name ~ sports) => Member(
id, name
sports.validate[List[Sport]].getOrElse(List())
)
}
}
def getMember():List[Member] = DB.WithConnection{ implicit c =>
SQL("SELECT m.id, m.name, m.sports::json from member m ").as(memberParser.*)
}
|运动|是一个jsonb postgresql列,如果我不将此列强制转换为::json
,我会收到TypeDoesNotMatc
h错误。
Json字段命名为" f1"," f2"," f3"(...)因为我已使用另一个表中的数据填充此列JSON_AGG(...
所以它的postgresql命名。
这个功能也在起作用:
def getMemberWithSports():List[Member] = DB.withConnection{ implicit c =>
SQL("""
SELECT m.member_id, m.name,
json_agg(row_to_json(row(s.sport_id, s.name))) as sports
FROM rank_member m
LEFT JOIN member_sport ms ON m.member_id = ms.member_id
LEFT JOIN sport s ON ms.sport_id = s.sport_id
GROUP BY m.member_id, m.fullname
""").as(memberParser.*)
}
这两个SQL命令(在getMember()& getMemberWithSport()中)的结果之间的唯一区别是,在第一个查询中我得不到运动中的第二个查询(在游戏中工作的那个)返回JSON填充空值。
答案 0 :(得分:-1)
目前我发现的唯一解决方法是将COALESCE添加到SQL查询中,如下所示:
def getMember():List[Member] = DB.WithConnection{ implicit c =>
SQL("SELECT m.id, m.name, COALESCE(m.sports::json, '[]') as sports from member m ").as(memberParser.*)
}