slick-pg icon indicating copy to clipboard operation
slick-pg copied to clipboard

cannot be cast to slick.ast.OptionType

Open 4x4notfound opened this issue 9 years ago • 4 comments

I'm trying to build a query that returns a few selected columns using slick-pg for as my slick driver but I'm having issues trying to map on the json column MyValueObject. The error I am getting at runtime is...

A client error occurred: slick.driver.JdbcTypesComponent$MappedJdbcType$$anon$1 cannot be cast to slick.ast.OptionType"

My case case clase, table mapper, and query looks similar to this...

case class MyAggregateObject(foo: String, valueObj: Option[MyJsonObject)
case class MyJsonObject(bar: Int, baz: Option[String] = None)
implicit val jsonObjectMapper = MappedColumnType.base[Option[MyJsonObject], JsValue](s => Json.toJson(s),str => Json.fromJson[MyJsonObject](str).asOpt)
val q = for {
  list <- myAggregateObjects
} yield (
  prs.foo,
  prs.MyJsonObject.flatMap(_.baz.bind)) // throws the error!

The only solution I can come up with right now is to map on the result but the returning rows can be very large and I'd like to avoid loading a sequence of large objects into memory.

Is there a way to fix this query?

stackoverflow: http://stackoverflow.com/questions/36703548/slick-query-jsonb-cannot-be-cast-to-slick-ast-optiontype

4x4notfound avatar Apr 18 '16 20:04 4x4notfound

I don't know more details about your case, so I can't reproduce it on my local.

But, why you don't declare the jsonObjectMapper as:

implicit val jsonObjectMapper = MappedColumnType.base[MyJsonObject, JsValue](s => Json.toJson(s),str => Json.fromJson[MyJsonObject](str))

As you know, slick will generate the option version of the mapper on demand for us.

tminglei avatar Apr 19 '16 02:04 tminglei

just drop the optional? I have it optional atm because the aggregate is created first without any data. I think I can work around that and try.

4x4notfound avatar Apr 19 '16 17:04 4x4notfound

I don't know what exactly happened, so I just pointed out an obviously problem. You can try it first.

If the problem still exists, pls provide more full details, to let me reproduce and deep it.

tminglei avatar Apr 20 '16 01:04 tminglei

@adrianmaurer All of the BaseTypedType can not to be Option[_], it's hard code in slick. Because can't read null data in all BaseTypedType. If you defined a Option BaseTypedType slick will always throws a error. Just do as what @tminglei said and slick will auto import a Shape[Rep[Option[MyJsonObject], Option[MyJsonObject], Rep[Option[MyJsonObject]]]] to you.

djx314 avatar Oct 12 '16 14:10 djx314