1

I'm using Squeryl 0.9.5-7 and Postgres 9.4 with jsonb datatype and want to insert some data:

case class Log(id: String, meta: String) //meta will contain json
val logs = table[Log]
logs.insert(Log(randomId, "{\"Hi\": \"I'm a json!\"}"))

But got a typecast error that says "Column meta has jsonb type but expression has character varying type. Rewrite expression or convert it's type."

How can I explicitly cast my String field into jsonb so that raw sql-parameter will look like ?::jsonb?

And then, it's interesting how to write json-queries such as @> or ->> with Squeryl?

plomovtsev
  • 512
  • 4
  • 11

3 Answers3

3

I found a better way, that doesn't require to create a CAST in the database.

If you're using squeryl 0.9.6, you can add an explicit cast in your schema which tells squeryl to explicitly cast your string into a jsonb.

on(logs)(s => declare(
   s.meta is (dbType("jsonb").explicitCast) // enables explicit casting into jsonb
 ))
Boris
  • 601
  • 3
  • 13
0

With Squeryl 0.9.6 you can register support for your own custom types. Here's an example. For non standard operators, take a look at custom functions

Dave Whittaker
  • 3,102
  • 13
  • 14
0

In my experience with postgres 9.4, reading works fine as a Squeryl String but inserting fails with:

ERROR: column "****" is of type json but expression is of type character varying [error] Hint: You will need to rewrite or cast the expression. [error] Position: 166 [error] errorCode: 0, sqlState: 42804

So, the solution I found is to create a 'AS ASSIGNMENT' cast in my postgres database and that does the trick:

CREATE CAST(VARCHAR AS JSON)
WITH INOUT
AS ASSIGNMENT
Boris
  • 601
  • 3
  • 13